Skip to content

Instantly share code, notes, and snippets.

@phillipsharring
Created August 5, 2015 15:29
Show Gist options
  • Save phillipsharring/bdc0e5bc3802c5228509 to your computer and use it in GitHub Desktop.
Save phillipsharring/bdc0e5bc3802c5228509 to your computer and use it in GitHub Desktop.
<?php
// app/providers/AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider {
// other code...
public function register()
{
// bind CommandTranslator interface to your implementation
$this->app->bind(
'Laracasts\Commander\CommandTranslator',
'App\Commands\CommandTranslator'
);
// other code...
}
// other code...
}
<?php
// app/Commmands/CommandTranslator.php
namespace App\Commands;
use Laracasts\Commander\CommandTranslator as CommandTranslatorInterface;
use Laracasts\Commander\HandlerNotRegisteredException;
class CommandTranslator implements CommandTranslatorInterface
{
public function toCommandHandler($command)
{
$commandClass = get_class($command);
// this line is a little ridiculous with the substr_replace, and could probably be improved.
$handler = 'App\\' . substr_replace($commandClass, 'Handlers\Commands\\', strpos('Commands\\', $commandClass), 13) . 'Handler';
if (!class_exists($handler)) {
$message = "Command handler [$handler] does not exist.";
throw new HandlerNotRegisteredException($message);
}
return $handler;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment