diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5826402 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..aa14ee5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - hhvm + +before_script: + - composer self-update + - composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c59e76e --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "laracasts/commander", + "description": "", + "authors": [ + { + "name": "Jeffrey Way", + "email": "jeffrey@laracasts.com" + } + ], + "require": { + "php": ">=5.3.0", + "illuminate/support": "4.1.*" + }, + "autoload": { + "psr-0": { + "Laracasts\\Commander": "src/" + } + }, + "minimum-stability": "stable" +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3347b75 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/src/Laracasts/Commander/BasicCommandTranslator.php b/src/Laracasts/Commander/BasicCommandTranslator.php new file mode 100644 index 0000000..64c6e1e --- /dev/null +++ b/src/Laracasts/Commander/BasicCommandTranslator.php @@ -0,0 +1,26 @@ +registerCommandTranslator(); + + $this->registerCommandBus(); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['commander']; + } + + private function registerCommandTranslator() + { + $this->app->bind('Laracasts\Commander\CommandTranslator', 'Laracasts\Commander\BasicCommandTranslator'); + } + + private function registerCommandBus() + { + $this->app->bindShared('Laracasts\Commander\CommandBus', function () + { + return $this->app->make('Laracasts\Commander\ValidationCommandBus'); + }); + } + +} diff --git a/src/Laracasts/Commander/DefaultCommandBus.php b/src/Laracasts/Commander/DefaultCommandBus.php new file mode 100644 index 0000000..fe8bb06 --- /dev/null +++ b/src/Laracasts/Commander/DefaultCommandBus.php @@ -0,0 +1,38 @@ +app = $app; + $this->commandTranslator = $commandTranslator; + } + + /** + * @param $command + * @return mixed + */ + public function execute($command) + { + $handler = $this->commandTranslator->toCommandHandler($command); + + return $this->app->make($handler)->handle($command); + } + +} \ No newline at end of file diff --git a/src/Laracasts/Commander/Events/EventDispatcher.php b/src/Laracasts/Commander/Events/EventDispatcher.php new file mode 100644 index 0000000..14cd42f --- /dev/null +++ b/src/Laracasts/Commander/Events/EventDispatcher.php @@ -0,0 +1,35 @@ +event = $event; + $this->log = $log; + } + + public function dispatch(array $events) + { + foreach($events as $event) + { + $eventName = $this->getEventName($event); + + $this->event->fire($eventName, $event); + + $this->log->info("$eventName was fired."); + } + } + + protected function getEventName($event) + { + return str_replace('\\', '.', get_class($event)); + } + +} \ No newline at end of file diff --git a/src/Laracasts/Commander/Events/EventGenerator.php b/src/Laracasts/Commander/Events/EventGenerator.php new file mode 100644 index 0000000..8b7386f --- /dev/null +++ b/src/Laracasts/Commander/Events/EventGenerator.php @@ -0,0 +1,21 @@ +pendingEvents[] = $event; + } + + public function releaseEvents() + { + $events = $this->pendingEvents; + + $this->pendingEvents = []; + + return $events; + } + +} \ No newline at end of file diff --git a/src/Laracasts/Commander/Events/EventListener.php b/src/Laracasts/Commander/Events/EventListener.php new file mode 100644 index 0000000..90c6a60 --- /dev/null +++ b/src/Laracasts/Commander/Events/EventListener.php @@ -0,0 +1,36 @@ +getEventName($event); + + if ($this->listenerIsRegistered($eventName)) + { + return call_user_func([$this, 'when'.$eventName], $event); + } + } + + /** + * @param $event + * @return string + */ + protected function getEventName($event) + { + return (new ReflectionClass($event))->getShortName(); + } + + /** + * @param $eventName + * @return bool + */ + protected function listenerIsRegistered($eventName) + { + $method = "when{$eventName}"; + + return method_exists($this, $method); + } +} \ No newline at end of file diff --git a/src/Laracasts/Commander/ValidationCommandBus.php b/src/Laracasts/Commander/ValidationCommandBus.php new file mode 100644 index 0000000..02cd36e --- /dev/null +++ b/src/Laracasts/Commander/ValidationCommandBus.php @@ -0,0 +1,17 @@ +commandTranslator->toValidator($command); + + if (class_exists($validator)) + { + $this->app->make($validator)->validate($command); + } + + return parent::execute($command); + } + +} \ No newline at end of file diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29