Skip to content

Commit 5014f97

Browse files
committed
Initial commit
0 parents  commit 5014f97

File tree

9 files changed

+1036
-0
lines changed

9 files changed

+1036
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

Diff for: Bootstraps/Zf2.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace PHPPM\Bootstraps;
4+
5+
use Zend\Mvc\Application;
6+
7+
class Zf2 implements BootstrapInterface
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $appenv;
13+
14+
/**
15+
* Instantiate the bootstrap, storing the $appenv.
16+
*
17+
* @param string $appenv
18+
*/
19+
public function __construct($appenv)
20+
{
21+
$this->appenv = $appenv;
22+
}
23+
24+
/**
25+
* Create a Zend Framework MVC application.
26+
*/
27+
public function getApplication()
28+
{
29+
if ($this->appenv) {
30+
$filename = "./config/{$this->appenv}.config.php";
31+
32+
} else {
33+
$filename = "./config/application.config.php";
34+
}
35+
36+
if (!file_exists($filename)) {
37+
throw new \RuntimeException("Configuration file {$filename} not found.");
38+
}
39+
40+
$config = require $filename;
41+
42+
$config['service_manager'] = array(
43+
'factories' => array(
44+
'ServiceListener' => 'PHPPM\Bootstraps\Zf2\Mvc\Service\ServiceListenerFactory',
45+
),
46+
);
47+
48+
return Application::init($config);
49+
}
50+
}

Diff for: Bootstraps/Zf2/Mvc/Application.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PHPPM\Bootstraps\Zf2\Mvc;
4+
5+
use Zend\ServiceManager\ServiceManager;
6+
use Zend\Mvc;
7+
8+
class Application extends Mvc\Application
9+
{
10+
11+
protected $defaultListeners = array(
12+
'RouteListener',
13+
'DispatchListener',
14+
'ViewManager',
15+
);
16+
17+
public function __construct($configuration, ServiceManager $serviceManager)
18+
{
19+
$this->configuration = $configuration;
20+
$this->serviceManager = $serviceManager;
21+
22+
$this->setEventManager($serviceManager->get('EventManager'));
23+
}
24+
25+
public function bootstrap(array $listeners = array())
26+
{
27+
$services = $this->serviceManager;
28+
$events = $this->events;
29+
30+
$listeners = array_unique(array_merge($this->defaultListeners, $listeners));
31+
32+
foreach ($listeners as $listener) {
33+
$events->attach($services->get($listener));
34+
}
35+
36+
// Setup MVC Event
37+
$this->event = $event = new Mvc\MvcEvent();
38+
$event->setTarget($this);
39+
$event->setApplication($this)
40+
->setRouter($services->get('Router'));
41+
42+
// Trigger bootstrap events
43+
$events->trigger(Mvc\MvcEvent::EVENT_BOOTSTRAP, $event);
44+
return $this;
45+
}
46+
47+
/**
48+
* @param \Zend\Http\PhpEnvironment\Request $request
49+
* @param \Zend\Http\PhpEnvironment\Response $response
50+
*/
51+
public function run()
52+
{
53+
$this->request = func_get_arg(0);
54+
$this->response = func_get_arg(1);
55+
56+
return parent::run();
57+
}
58+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PHPPM\Bootstraps\Zf2\Mvc\Service;
4+
5+
use Zend\Mvc;
6+
use Zend\ServiceManager\ServiceLocatorInterface;
7+
use PHPPM\Bootstraps\Zf2\Mvc\Application;
8+
use Zend\Http\PhpEnvironment\Response;
9+
use Zend\Http\PhpEnvironment\Request;
10+
11+
class ServiceListenerFactory extends Mvc\Service\ServiceListenerFactory
12+
{
13+
public function __construct()
14+
{
15+
$this->defaultServiceConfig['factories'] = array_merge($this->defaultServiceConfig['factories'], array(
16+
'ViewHelperManager' => 'PHPPM\Bootstraps\Zf2\Mvc\Service\ViewHelperManagerFactory',
17+
18+
'Router' => function(ServiceLocatorInterface $serviceLocator) {
19+
$config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();
20+
21+
// Defaults
22+
$routerClass = 'Zend\Mvc\Router\Http\TreeRouteStack';
23+
$routerConfig = isset($config['router']) ? $config['router'] : array();
24+
25+
// Obtain the configured router class, if any
26+
if (isset($routerConfig['router_class']) && class_exists($routerConfig['router_class'])) {
27+
$routerClass = $routerConfig['router_class'];
28+
}
29+
30+
// Inject the route plugins
31+
if (!isset($routerConfig['route_plugins'])) {
32+
$routePluginManager = $serviceLocator->get('RoutePluginManager');
33+
$routerConfig['route_plugins'] = $routePluginManager;
34+
}
35+
36+
// Obtain an instance
37+
$factory = sprintf('%s::factory', $routerClass);
38+
return call_user_func($factory, $routerConfig);
39+
},
40+
41+
'Request' => function(ServiceLocatorInterface $serviceLocator) {
42+
return new Request();
43+
},
44+
45+
'Response' => function(ServiceLocatorInterface $serviceLocator) {
46+
return new Response();
47+
},
48+
49+
'Application' => function(ServiceLocatorInterface $serviceLocator) {
50+
return new Application($serviceLocator->get('Config'), $serviceLocator);
51+
},
52+
53+
'ViewManager' => function(ServiceLocatorInterface $serviceLocator) {
54+
return $serviceLocator->get('HttpViewManager');
55+
},
56+
));
57+
}
58+
}
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace PHPPM\Bootstraps\Zf2\Mvc\Service;
4+
5+
use Zend\Mvc;
6+
use Zend\Mvc\Exception;
7+
use Zend\Mvc\Router\RouteMatch;
8+
use Zend\ServiceManager\ConfigInterface;
9+
use Zend\ServiceManager\ServiceLocatorInterface;
10+
use Zend\View\Helper as ViewHelper;
11+
use Zend\View\Helper\HelperInterface as ViewHelperInterface;
12+
13+
class ViewHelperManagerFactory extends Mvc\Service\ViewHelperManagerFactory
14+
{
15+
public function createService(ServiceLocatorInterface $serviceLocator)
16+
{
17+
$plugins = parent::createService($serviceLocator);
18+
19+
foreach ($this->defaultHelperMapClasses as $configClass) {
20+
if (is_string($configClass) && class_exists($configClass)) {
21+
$config = new $configClass;
22+
23+
if (!$config instanceof ConfigInterface) {
24+
throw new Exception\RuntimeException(sprintf(
25+
'Invalid service manager configuration class provided; received "%s", expected class implementing %s',
26+
$configClass,
27+
'Zend\ServiceManager\ConfigInterface'
28+
));
29+
}
30+
31+
$config->configureServiceManager($plugins);
32+
}
33+
}
34+
35+
// Configure URL view helper with router
36+
$plugins->setFactory('url', function ($sm) use ($serviceLocator) {
37+
$helper = new ViewHelper\Url;
38+
$router = 'Router';
39+
$helper->setRouter($serviceLocator->get($router));
40+
41+
$match = $serviceLocator->get('application')
42+
->getMvcEvent()
43+
->getRouteMatch()
44+
;
45+
46+
if ($match instanceof RouteMatch) {
47+
$helper->setRouteMatch($match);
48+
}
49+
50+
return $helper;
51+
});
52+
53+
$plugins->setFactory('basepath', function ($sm) use ($serviceLocator) {
54+
$config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();
55+
$basePathHelper = new ViewHelper\BasePath;
56+
if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
57+
$basePathHelper->setBasePath($config['view_manager']['base_path']);
58+
} else {
59+
$request = $serviceLocator->get('Request');
60+
if (is_callable(array($request, 'getBasePath'))) {
61+
$basePathHelper->setBasePath($request->getBasePath());
62+
}
63+
}
64+
65+
return $basePathHelper;
66+
});
67+
68+
/**
69+
* Configure doctype view helper with doctype from configuration, if available.
70+
*
71+
* Other view helpers depend on this to decide which spec to generate their tags
72+
* based on. This is why it must be set early instead of later in the layout phtml.
73+
*/
74+
$plugins->setFactory('doctype', function ($sm) use ($serviceLocator) {
75+
$config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();
76+
$config = isset($config['view_manager']) ? $config['view_manager'] : array();
77+
$doctypeHelper = new ViewHelper\Doctype;
78+
if (isset($config['doctype']) && $config['doctype']) {
79+
$doctypeHelper->setDoctype($config['doctype']);
80+
}
81+
return $doctypeHelper;
82+
});
83+
84+
return $plugins;
85+
}
86+
}

Diff for: Bridges/Zf2.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace PHPPM\Bridges;
4+
5+
use React\Http\Request as ReactRequest;
6+
use React\Http\Response as ReactResponse;
7+
use Zend\Http\PhpEnvironment\Request as ZendRequest;
8+
use Zend\Http\PhpEnvironment\Response as ZendResponse;
9+
use Zend\Http\Headers as ZendHeaders;
10+
use Zend\Stdlib\Parameters;
11+
use Zend\Mvc\MvcEvent;
12+
use Zend\Mvc\SendResponseListener;
13+
use Zend\Mvc\ResponseSender\SendResponseEvent;
14+
15+
class Zf2 implements BridgeInterface
16+
{
17+
/**
18+
* @var \Zend\Mvc\Application
19+
*/
20+
protected $application;
21+
22+
/**
23+
* @param string $appBootstrap
24+
* @param string $appenv
25+
*/
26+
public function bootstrap($appBootstrap, $appenv)
27+
{
28+
/* @var $bootstrap \PHPPM\Bootstraps\Zf2 */
29+
$bootstrap = new \PHPPM\Bootstraps\Zf2($appenv);
30+
$this->application = $bootstrap->getApplication();
31+
}
32+
33+
/**
34+
* Handle a request using Zend\Mvc\Application.
35+
*
36+
* @param ReactRequest $request
37+
* @param ReactResponse $response
38+
*/
39+
public function onRequest(ReactRequest $request, ReactResponse $response)
40+
{
41+
if (null === ($app = $this->application)) {
42+
return;
43+
}
44+
45+
/* @var $sm \Zend\ServiceManager\ServiceManager */
46+
$sm = $app->getServiceManager();
47+
48+
$zfRequest = new ZendRequest();
49+
$zfResponse = new ZendResponse();
50+
51+
self::mapRequest($request, $zfRequest);
52+
53+
$sm->setAllowOverride(true);
54+
$sm->setService('Request', $zfRequest);
55+
$sm->setService('Response', $zfResponse);
56+
$sm->setAllowOverride(false);
57+
58+
$event = $app->getMvcEvent();
59+
$event->setRequest($zfRequest);
60+
$event->setResponse($zfResponse);
61+
62+
try {
63+
$app->run($zfRequest, $zfResponse);
64+
} catch (\Exception $exception) {
65+
$response->writeHead(500); // internal server error
66+
$response->end();
67+
return;
68+
}
69+
70+
self::mapResponse($response, $zfResponse);
71+
}
72+
73+
/**
74+
* @param ReactRequest $reactRequest
75+
* @param ZendRequest $zfRequest
76+
*/
77+
protected static function mapRequest(ReactRequest $reactRequest,
78+
ZendRequest $zfRequest)
79+
{
80+
$headers = new ZendHeaders();
81+
$headers->addHeaders($reactRequest->getHeaders());
82+
83+
$query = new Parameters();
84+
$query->fromArray($reactRequest->getQuery());
85+
86+
$zfRequest->setHeaders($headers);
87+
$zfRequest->setQuery($query);
88+
$zfRequest->setMethod($reactRequest->getMethod());
89+
$zfRequest->setUri($reactRequest->getPath());
90+
$zfRequest->setRequestUri($reactRequest->getPath());
91+
92+
$server = $zfRequest->getServer();
93+
$server->set('REQUEST_URI', $reactRequest->getPath());
94+
$server->set('SERVER_NAME', $zfRequest->getHeader('Host'));
95+
}
96+
97+
/**
98+
* @param ReactResponse $reactResponse
99+
* @param ZendResponse $zfResponse
100+
*/
101+
protected static function mapResponse(ReactResponse $reactResponse,
102+
ZendResponse $zfResponse)
103+
{
104+
$headers = array_map('current', $zfResponse->getHeaders()->toArray());
105+
$reactResponse->writeHead($zfResponse->getStatusCode(), $headers);
106+
$reactResponse->end($zfResponse->getContent());
107+
}
108+
}

Diff for: README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# PHP-PM Zend Adapter
2+
3+
Zend application framework adapter for PHP-PM. See https://github.com/php-pm/php-pm.
4+
5+
### Setup
6+
7+
1. Install PHP-PM
8+
9+
composer require php-pm/php-pm:dev-master
10+
11+
2. Install Zend Adapter
12+
13+
composer require php-pm/zend-adapter:dev-master
14+

Diff for: composer.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "php-pm/zend-adapter",
3+
"require": {
4+
"zendframework/zend-http": "2.3.*",
5+
"zendframework/zend-mvc": "2.3.*"
6+
},
7+
"autoload": {
8+
"psr-4": {
9+
"PHPPM\\": ""
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)