Created
August 5, 2015 15:29
-
-
Save phillipsharring/bdc0e5bc3802c5228509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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