-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 709cb96
Showing
15 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.DS_Store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "laracasts/commander", | ||
"description": "", | ||
"authors": [ | ||
{ | ||
"name": "Jeffrey Way", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"illuminate/support": "4.1.*" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"Laracasts\\Commander": "src/" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
use Exception; | ||
|
||
class BasicCommandTranslator implements CommandTranslator { | ||
|
||
public function toCommandHandler($command) | ||
{ | ||
$handler = str_replace('Command', 'CommandHandler', get_class($command)); | ||
|
||
if ( ! class_exists($handler)) | ||
{ | ||
$message = "Command handler [$handler] does not exist."; | ||
|
||
throw new Exception($message); | ||
} | ||
|
||
return $handler; | ||
} | ||
|
||
public function toValidator($command) | ||
{ | ||
return str_replace('Command', 'Validator', get_class($command)); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
interface CommandBus { | ||
|
||
public function execute($command); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
interface CommandHandler { | ||
|
||
/** | ||
* Handle the command | ||
* | ||
* @param $command | ||
* @return mixed | ||
*/ | ||
public function handle($command); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
interface CommandTranslator { | ||
|
||
public function toCommandHandler($command); | ||
|
||
public function toValidator($command); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CommanderServiceProvider extends ServiceProvider { | ||
|
||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->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'); | ||
}); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
use Illuminate\Foundation\Application; | ||
|
||
class DefaultCommandBus implements CommandBus { | ||
|
||
/** | ||
* @var Application | ||
*/ | ||
protected $app; | ||
|
||
/** | ||
* @var CommandTranslator | ||
*/ | ||
protected $commandTranslator; | ||
|
||
/** | ||
* @param Application $app | ||
* @param CommandTranslator $commandTranslator | ||
*/ | ||
function __construct(Application $app, CommandTranslator $commandTranslator) | ||
{ | ||
$this->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); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php namespace Laracasts\Commander\Events; | ||
|
||
use Illuminate\Events\Dispatcher; | ||
use Illuminate\Log\Writer; | ||
|
||
class EventDispatcher { | ||
|
||
protected $event; | ||
|
||
protected $log; | ||
|
||
function __construct(Dispatcher $event, Writer $log) | ||
{ | ||
$this->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)); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php namespace Laracasts\Commander\Events; | ||
|
||
trait EventGenerator { | ||
|
||
protected $pendingEvents = []; | ||
|
||
public function raise($event) | ||
{ | ||
$this->pendingEvents[] = $event; | ||
} | ||
|
||
public function releaseEvents() | ||
{ | ||
$events = $this->pendingEvents; | ||
|
||
$this->pendingEvents = []; | ||
|
||
return $events; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php namespace Laracasts\Commander\Events; | ||
|
||
use ReflectionClass; | ||
|
||
abstract class EventListener { | ||
|
||
public function handle($event) | ||
{ | ||
$eventName = $this->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); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php namespace Laracasts\Commander; | ||
|
||
class ValidationCommandBus extends DefaultCommandBus { | ||
|
||
public function execute($command) | ||
{ | ||
$validator = $this->commandTranslator->toValidator($command); | ||
|
||
if (class_exists($validator)) | ||
{ | ||
$this->app->make($validator)->validate($command); | ||
} | ||
|
||
return parent::execute($command); | ||
} | ||
|
||
} |
Empty file.