Skip to content

Commit 4b552a6

Browse files
committed
Enabled pi command line pi-engine#1404
1 parent cab82fa commit 4b552a6

File tree

9 files changed

+720
-0
lines changed

9 files changed

+720
-0
lines changed

lib/Pi/Application/Engine/Command.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Pi Engine (http://pialog.org)
4+
*
5+
* @link http://code.pialog.org for the Pi Engine source repository
6+
* @copyright Copyright (c) Pi Engine http://pialog.org
7+
* @license http://pialog.org/license.txt BSD 3-Clause License
8+
*/
9+
10+
namespace Pi\Application\Engine;
11+
12+
use Pi\Command\Mvc\Application;
13+
14+
/**
15+
* Pi command line application engine
16+
*
17+
* How to use command line:
18+
* path/to/www/pi {module}/{controller}/{action} param1 param2
19+
*
20+
* @author Zongshu Lin <lin40553024@163.com>
21+
*/
22+
class Command extends Standard
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
const SECTION = 'command';
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
protected $fileIdentifier = 'command';
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function application()
38+
{
39+
if (!$this->application) {
40+
$options = isset($this->options['application'])
41+
? $this->options['application'] : array();
42+
$this->application = Application::load($options);
43+
$this->application->setEngine($this)->setSection($this->section());
44+
}
45+
46+
return $this->application;
47+
}
48+
}

lib/Pi/Command/Mvc/Application.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Pi Engine (http://pialog.org)
4+
*
5+
* @link http://code.pialog.org for the Pi Engine source repository
6+
* @copyright Copyright (c) Pi Engine http://pialog.org
7+
* @license http://pialog.org/license.txt BSD 3-Clause License
8+
*/
9+
10+
namespace Pi\Command\Mvc;
11+
12+
use Pi;
13+
use Pi\Application\Engine\AbstractEngine;
14+
use Pi\Mvc\Application as PiApplication;
15+
use Zend\Mvc\MvcEvent;
16+
use Zend\Mvc\Service;
17+
use Zend\ServiceManager\ServiceManager;
18+
19+
/**
20+
* Command line Application handler
21+
*
22+
* {@inheritDoc}
23+
* @author Zongshu Lin <lin40553024@163.com>
24+
*/
25+
class Application extends PiApplication
26+
{
27+
// Default listenser, @see Zend\Mvc\Application
28+
29+
/**
30+
* Load application handler
31+
*
32+
* @param array $configuration
33+
* @return $this
34+
*/
35+
public static function load($configuration = array())
36+
{
37+
$smConfig = isset($configuration['service_manager'])
38+
? $configuration['service_manager'] : array();
39+
$listeners = isset($configuration['listeners'])
40+
? $configuration['listeners'] : array();
41+
$serviceManager = new ServiceManager(
42+
new Service\ServiceManagerConfig($smConfig)
43+
);
44+
45+
return $serviceManager->get('Application')->setListeners($listeners);
46+
}
47+
48+
/**
49+
* Bootstrap application
50+
*
51+
* @param array $listeners
52+
* @return \Pi\Command\Mvc\Application
53+
*/
54+
public function bootstrap(array $listeners = array())
55+
{
56+
$serviceManager = $this->serviceManager;
57+
$events = $this->events;
58+
59+
$listeners = array_unique(array_merge($this->defaultListeners, $listeners));
60+
61+
foreach ($listeners as $listener) {
62+
$events->attach($serviceManager->get($listener));
63+
}
64+
65+
// Set custom router
66+
$router = $serviceManager->get('ConsoleRouter');
67+
$router->addRoute('Standard', [
68+
'name' => 'default',
69+
'type' => 'Pi\Command\Mvc\Router\Http\Standard',
70+
'options' => [
71+
'route' => 'default',
72+
],
73+
], 0);
74+
75+
// Setup MVC Event
76+
$this->event = $event = new MvcEvent();
77+
$event->setTarget($this);
78+
$event->setApplication($this)
79+
->setRequest($this->request)
80+
->setRouter($router);
81+
82+
// Trigger bootstrap events
83+
$events->trigger(MvcEvent::EVENT_BOOTSTRAP, $event);
84+
return $this;
85+
}
86+
}

lib/Pi/Command/Mvc/RouteListener.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Pi\Command\Mvc;
11+
12+
use Zend\EventManager\AbstractListenerAggregate;
13+
use Zend\EventManager\EventManagerInterface;
14+
use Zend\Mvc\MvcEvent;
15+
use Zend\Mvc\Router\Console\RouteMatch;
16+
17+
/**
18+
* Route listener for command line
19+
*
20+
* @author Zongshu Lin <lin40553024@163.com>
21+
*/
22+
class RouteListener extends AbstractListenerAggregate
23+
{
24+
/**
25+
* Attach to an event manager
26+
*
27+
* @param EventManagerInterface $events
28+
* @return void
29+
*/
30+
public function attach(EventManagerInterface $events)
31+
{
32+
$this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'));
33+
}
34+
35+
/**
36+
* Listen to the "route" event and attempt to route the request
37+
*
38+
* If no matches are returned, triggers "dispatch.error" in order to
39+
* create a 404 response.
40+
*
41+
* Seeds the event with the route match on completion.
42+
*
43+
* @param MvcEvent $e
44+
* @return null|Router\RouteMatch
45+
*/
46+
public function onRoute($e)
47+
{
48+
$target = $e->getTarget();
49+
$request = $e->getRequest();
50+
$router = $e->getRouter();
51+
$routeMatch = $router->match($request);
52+
53+
if (!$routeMatch instanceof RouteMatch) {
54+
$e->setError(Application::ERROR_ROUTER_NO_MATCH);
55+
56+
$results = $target->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
57+
if (count($results)) {
58+
$return = $results->last();
59+
} else {
60+
$return = $e->getParams();
61+
}
62+
return $return;
63+
}
64+
65+
$e->setRouteMatch($routeMatch);
66+
return $routeMatch;
67+
}
68+
}

0 commit comments

Comments
 (0)