Skip to content

Commit

Permalink
Merge pull request #1405 from linzongshu/develop
Browse files Browse the repository at this point in the history
Enabled pi command line #1404
  • Loading branch information
taiwen committed Apr 27, 2016
2 parents cab82fa + 4b552a6 commit 003a92f
Show file tree
Hide file tree
Showing 9 changed files with 720 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/Pi/Application/Engine/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Pi Engine (http://pialog.org)
*
* @link http://code.pialog.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://pialog.org
* @license http://pialog.org/license.txt BSD 3-Clause License
*/

namespace Pi\Application\Engine;

use Pi\Command\Mvc\Application;

/**
* Pi command line application engine
*
* How to use command line:
* path/to/www/pi {module}/{controller}/{action} param1 param2
*
* @author Zongshu Lin <[email protected]>
*/
class Command extends Standard
{
/**
* {@inheritDoc}
*/
const SECTION = 'command';

/**
* {@inheritDoc}
*/
protected $fileIdentifier = 'command';

/**
* {@inheritDoc}
*/
public function application()
{
if (!$this->application) {
$options = isset($this->options['application'])
? $this->options['application'] : array();
$this->application = Application::load($options);
$this->application->setEngine($this)->setSection($this->section());
}

return $this->application;
}
}
86 changes: 86 additions & 0 deletions lib/Pi/Command/Mvc/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Pi Engine (http://pialog.org)
*
* @link http://code.pialog.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://pialog.org
* @license http://pialog.org/license.txt BSD 3-Clause License
*/

namespace Pi\Command\Mvc;

use Pi;
use Pi\Application\Engine\AbstractEngine;
use Pi\Mvc\Application as PiApplication;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Service;
use Zend\ServiceManager\ServiceManager;

/**
* Command line Application handler
*
* {@inheritDoc}
* @author Zongshu Lin <[email protected]>
*/
class Application extends PiApplication
{
// Default listenser, @see Zend\Mvc\Application

/**
* Load application handler
*
* @param array $configuration
* @return $this
*/
public static function load($configuration = array())
{
$smConfig = isset($configuration['service_manager'])
? $configuration['service_manager'] : array();
$listeners = isset($configuration['listeners'])
? $configuration['listeners'] : array();
$serviceManager = new ServiceManager(
new Service\ServiceManagerConfig($smConfig)
);

return $serviceManager->get('Application')->setListeners($listeners);
}

/**
* Bootstrap application
*
* @param array $listeners
* @return \Pi\Command\Mvc\Application
*/
public function bootstrap(array $listeners = array())
{
$serviceManager = $this->serviceManager;
$events = $this->events;

$listeners = array_unique(array_merge($this->defaultListeners, $listeners));

foreach ($listeners as $listener) {
$events->attach($serviceManager->get($listener));
}

// Set custom router
$router = $serviceManager->get('ConsoleRouter');
$router->addRoute('Standard', [
'name' => 'default',
'type' => 'Pi\Command\Mvc\Router\Http\Standard',
'options' => [
'route' => 'default',
],
], 0);

// Setup MVC Event
$this->event = $event = new MvcEvent();
$event->setTarget($this);
$event->setApplication($this)
->setRequest($this->request)
->setRouter($router);

// Trigger bootstrap events
$events->trigger(MvcEvent::EVENT_BOOTSTRAP, $event);
return $this;
}
}
68 changes: 68 additions & 0 deletions lib/Pi/Command/Mvc/RouteListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Pi\Command\Mvc;

use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Console\RouteMatch;

/**
* Route listener for command line
*
* @author Zongshu Lin <[email protected]>
*/
class RouteListener extends AbstractListenerAggregate
{
/**
* Attach to an event manager
*
* @param EventManagerInterface $events
* @return void
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'));
}

/**
* Listen to the "route" event and attempt to route the request
*
* If no matches are returned, triggers "dispatch.error" in order to
* create a 404 response.
*
* Seeds the event with the route match on completion.
*
* @param MvcEvent $e
* @return null|Router\RouteMatch
*/
public function onRoute($e)
{
$target = $e->getTarget();
$request = $e->getRequest();
$router = $e->getRouter();
$routeMatch = $router->match($request);

if (!$routeMatch instanceof RouteMatch) {
$e->setError(Application::ERROR_ROUTER_NO_MATCH);

$results = $target->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
if (count($results)) {
$return = $results->last();
} else {
$return = $e->getParams();
}
return $return;
}

$e->setRouteMatch($routeMatch);
return $routeMatch;
}
}
Loading

0 comments on commit 003a92f

Please sign in to comment.