| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Controller |
| 12 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
* @version $Id$ |
| 15 |
|
*/ |
| 16 |
|
namespace Xyster\Controller\Dispatcher; |
| 17 |
|
/** |
| 18 |
|
* A dispatcher that creates action controllers out of a Xyster_Container. |
| 19 |
|
* |
| 20 |
|
* Controllers are injected using autowiring by type. Any setter method that |
| 21 |
|
* accepts a non-scalar or non-array value will be autowired (except for the |
| 22 |
|
* 'setResponse', 'setRequest', and 'setFrontController' methods). |
| 23 |
|
* |
| 24 |
|
* @category Xyster |
| 25 |
|
* @package Xyster_Controller |
| 26 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 27 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 28 |
|
*/ |
| 29 |
|
class Container extends \Zend_Controller_Dispatcher_Standard |
| 30 |
1 |
{ |
| 31 |
|
/** |
| 32 |
|
* @var \Xyster\Container\IContainer |
| 33 |
|
*/ |
| 34 |
|
protected $_container; |
| 35 |
|
|
| 36 |
|
/** |
| 37 |
|
* Constructor: Set current module to default value |
| 38 |
|
* |
| 39 |
|
* @param \Xyster\Container\IContainer $container |
| 40 |
|
* @param array $params |
| 41 |
|
*/ |
| 42 |
|
public function __construct(\Xyster\Container\IContainer $container, array $params = array()) |
| 43 |
|
{ |
| 44 |
2 |
$this->_container = $container; |
| 45 |
2 |
parent::__construct($params); |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
/** |
| 49 |
|
* Dispatch to a controller/action |
| 50 |
|
* |
| 51 |
|
* By default, if a controller is not dispatchable, dispatch() will throw |
| 52 |
|
* an exception. If you wish to use the default controller instead, set the |
| 53 |
|
* param 'useDefaultControllerAlways' via {@link setParam()}. |
| 54 |
|
* |
| 55 |
|
* @param Zend_Controller_Request_Abstract $request |
| 56 |
|
* @param Zend_Controller_Response_Abstract $response |
| 57 |
|
* @return boolean |
| 58 |
|
* @throws Zend_Controller_Dispatcher_Exception |
| 59 |
|
*/ |
| 60 |
|
public function dispatch(\Zend_Controller_Request_Abstract $request, \Zend_Controller_Response_Abstract $response) |
| 61 |
|
{ |
| 62 |
1 |
$this->setResponse($response); |
| 63 |
|
|
| 64 |
|
/** |
| 65 |
|
* Get controller class |
| 66 |
|
*/ |
| 67 |
1 |
if (!$this->isDispatchable($request)) { |
| 68 |
|
$controller = $request->getControllerName(); |
| 69 |
|
if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) { |
| 70 |
|
throw new \Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')'); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
$className = $this->getDefaultControllerClass($request); |
| 74 |
|
} else { |
| 75 |
1 |
$className = $this->getControllerClass($request); |
| 76 |
1 |
if (!$className) { |
| 77 |
|
$className = $this->getDefaultControllerClass($request); |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
/** |
| 82 |
|
* Load the controller class file |
| 83 |
|
*/ |
| 84 |
1 |
$className = $this->loadClass($className); |
| 85 |
|
|
| 86 |
|
/** |
| 87 |
|
* Instantiate controller with request, response, and invocation |
| 88 |
|
* arguments; throw exception if it's not an action controller |
| 89 |
|
*/ |
| 90 |
1 |
$injector = new \Xyster\Controller\Action\Injector( |
| 91 |
1 |
\Xyster\Container\Container::definition($className), $request, |
| 92 |
1 |
$this->getResponse(), $this->getParams()); |
| 93 |
1 |
$controller = $injector->get($this->_container); |
| 94 |
1 |
if (!($controller instanceof \Zend_Controller_Action_Interface) && |
| 95 |
1 |
!($controller instanceof \Zend_Controller_Action)) { |
| 96 |
|
throw new \Zend_Controller_Dispatcher_Exception( |
| 97 |
|
'Controller "' . $className . '" is not an instance of Zend_Controller_Action_Interface' |
| 98 |
|
); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* Retrieve the action name |
| 103 |
|
*/ |
| 104 |
1 |
$action = $this->getActionMethod($request); |
| 105 |
|
|
| 106 |
|
/** |
| 107 |
|
* Dispatch the method call |
| 108 |
|
*/ |
| 109 |
1 |
$request->setDispatched(true); |
| 110 |
|
|
| 111 |
|
// by default, buffer output |
| 112 |
1 |
$disableOb = $this->getParam('disableOutputBuffering'); |
| 113 |
1 |
$obLevel = ob_get_level(); |
| 114 |
1 |
if (empty($disableOb)) { |
| 115 |
1 |
ob_start(); |
| 116 |
1 |
} |
| 117 |
|
|
| 118 |
|
try { |
| 119 |
1 |
$controller->dispatch($action); |
| 120 |
1 |
} catch (Exception $e) { |
| 121 |
|
// Clean output buffer on error |
| 122 |
|
$curObLevel = ob_get_level(); |
| 123 |
|
if ($curObLevel > $obLevel) { |
| 124 |
|
do { |
| 125 |
|
ob_get_clean(); |
| 126 |
|
$curObLevel = ob_get_level(); |
| 127 |
|
} while ($curObLevel > $obLevel); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
throw $e; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
1 |
if (empty($disableOb)) { |
| 134 |
1 |
$content = ob_get_clean(); |
| 135 |
1 |
$response->appendBody($content); |
| 136 |
1 |
} |
| 137 |
|
|
| 138 |
|
// Destroy the page controller instance and reflection objects |
| 139 |
1 |
$controller = null; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
/** |
| 143 |
|
* Gets the container |
| 144 |
|
* |
| 145 |
|
* @return \Xyster\Container\IContainer |
| 146 |
|
*/ |
| 147 |
|
public function getContainer() |
| 148 |
|
{ |
| 149 |
1 |
return $this->_container; |
| 150 |
|
} |
| 151 |
1 |
} |