Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Jun 28, 2014
0 parents commit 709cb96
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
14 changes: 14 additions & 0 deletions .travis.yml
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
20 changes: 20 additions & 0 deletions composer.json
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"
}
18 changes: 18 additions & 0 deletions phpunit.xml
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>
26 changes: 26 additions & 0 deletions src/Laracasts/Commander/BasicCommandTranslator.php
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));
}

}
7 changes: 7 additions & 0 deletions src/Laracasts/Commander/CommandBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Laracasts\Commander;

interface CommandBus {

public function execute($command);

}
13 changes: 13 additions & 0 deletions src/Laracasts/Commander/CommandHandler.php
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);

}
8 changes: 8 additions & 0 deletions src/Laracasts/Commander/CommandTranslator.php
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);
}
49 changes: 49 additions & 0 deletions src/Laracasts/Commander/CommanderServiceProvider.php
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');
});
}

}
38 changes: 38 additions & 0 deletions src/Laracasts/Commander/DefaultCommandBus.php
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);
}

}
35 changes: 35 additions & 0 deletions src/Laracasts/Commander/Events/EventDispatcher.php
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));
}

}
21 changes: 21 additions & 0 deletions src/Laracasts/Commander/Events/EventGenerator.php
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;
}

}
36 changes: 36 additions & 0 deletions src/Laracasts/Commander/Events/EventListener.php
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);
}
}
17 changes: 17 additions & 0 deletions src/Laracasts/Commander/ValidationCommandBus.php
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 added tests/.gitkeep
Empty file.

0 comments on commit 709cb96

Please sign in to comment.