-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1405 from linzongshu/develop
Enabled pi command line #1404
- Loading branch information
Showing
9 changed files
with
720 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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.