Created
January 30, 2012 12:34
-
-
Save flavius/1704180 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
SetEnv APPLICATION_ENV development | |
RewriteEngine On | |
RewriteCond %{REQUEST_FILENAME} -s [OR] | |
RewriteCond %{REQUEST_FILENAME} -l [OR] | |
RewriteCond %{REQUEST_FILENAME} -d | |
RewriteRule ^.*$ - [NC,L] | |
RewriteRule ^.*$ index.php [NC,L] |
This file contains hidden or 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
<h1>An error occurred</h1> | |
<h2><?php echo $this->message ?></h2> | |
<?php if (isset($this->display_exceptions) && $this->display_exceptions): ?> | |
<?php if(isset($this->exception) && $this->exception instanceof Exception): ?> | |
<h3>Exception information:</h3> | |
<p> | |
<h4>Message:</h4> | |
<pre><?php echo $this->exception->getMessage() ?></pre> | |
<h4>Stack trace:</h4> | |
<pre><?php echo $this->exception->getTraceAsString() ?></pre> | |
</p> | |
<?php else: ?> | |
<h3>No Exception available</h3> | |
<?php endif ?> | |
<?php endif ?> |
This file contains hidden or 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
<?php | |
return array( | |
'modules' => array( | |
'Application', | |
), | |
'module_listener_options' => array( | |
'config_cache_enabled' => false, | |
'cache_dir' => 'data/cache', | |
'module_paths' => array( | |
'./modules', | |
), | |
) | |
); |
This file contains hidden or 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
<?php | |
chdir(dirname(__DIR__)); | |
require_once '/home/flav/projects/checkouts/zf2/library/Zend/Loader/AutoloaderFactory.php'; | |
\Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array())); | |
$appConfig = include 'config/application.config.php'; | |
$listenerOptions = new Zend\Module\Listener\ListenerOptions($appConfig['module_listener_options']); | |
$defaultListeners = new Zend\Module\Listener\DefaultListenerAggregate($listenerOptions); | |
//$defaultListeners->getConfigListener()->addConfigGlobPath('config/autoload/*.config.php'); | |
$moduleManager = new Zend\Module\Manager($appConfig['modules']); | |
unset($appConfig); | |
$moduleManager->events()->attachAggregate($defaultListeners); | |
//-------------------------------------------------- | |
$cb_loadmodules_pre = function($e) { | |
echo "loadModules.pre\n"; | |
}; | |
$cb_loadmodules_post = function($e) { | |
echo "loadModules.post\n"; | |
}; | |
$cb_loadmodule_resolve = function($e) { | |
echo "loadModule.resolve\n"; | |
}; | |
$cb_loadmodule= function($e) { | |
echo "loadModule\n"; | |
}; | |
$moduleManager->events()->attach('loadModules.pre', $cb_loadmodules_pre); | |
$moduleManager->events()->attach('loadModules.post', $cb_loadmodules_post); | |
$moduleManager->events()->attach('loadModule.resolve', $cb_loadmodule_resolve); | |
$moduleManager->events()->attach('loadModule', $cb_loadmodule); | |
echo '<pre>'; | |
//-------------------------------------------------- | |
$moduleManager->loadModules(); | |
$bootstrap = new Zend\Mvc\Bootstrap($defaultListeners->getConfigListener()->getMergedConfig()); | |
$application = new Zend\Mvc\Application; | |
//-------------------------------------------------- | |
$cb_route = function($e) { | |
echo "route\n"; | |
}; | |
$cb_dispatch = function($e) { | |
echo "dispatch\n"; | |
}; | |
$cb_bootstrap = function($e) { | |
$app = $e->getParam('application'); | |
$locator = $app->getLocator(); | |
//begin: set up view di | |
$im = $locator->instanceManager(); | |
$im->addAlias('view', 'Zend\View\PhpRenderer'); | |
$im->setParameters('Zend\View\PhpRenderer', array( | |
'resolver' => 'Zend\View\TemplatePathStack', | |
'options' => array( | |
'script_paths' => array( | |
'application' => __DIR__ . '/../modules/Application/views', | |
), | |
), | |
)); | |
//end: set up view di | |
$config = $e->getParam('config'); | |
$view = $locator->get('view'); | |
$url = $view->plugin('url'); | |
$url->setRouter($app->getRouter()); | |
//TODO more preparation for default layout | |
$viewListener = new Application\View\Listener($view, 'layout/layout.phtml'); | |
$app->events()->attachAggregate($viewListener); | |
$events = Zend\EventManager\StaticEventManager::getInstance(); | |
$viewListener->registerStaticListeners($events, $locator); | |
echo "bootstrap\n"; | |
//var_dump($locator); | |
}; | |
$cb_dispatch_error = function($e) { | |
echo "dispatch.error\n"; | |
//var_dump($e); | |
//$e->stopPropagation(); | |
}; | |
$cb_finish = function($e) { | |
echo "finish\n"; | |
var_dump($e->getParam('__RESULT__')); | |
}; | |
$events = $application->events(); | |
$events->attach('route', $cb_route); | |
$events->attach('dispatch', $cb_dispatch); | |
$events->attach('dispatch.error', $cb_dispatch_error); | |
$events->attach('finish', $cb_finish); | |
$bootstrap->events()->attach('bootstrap', $cb_bootstrap); | |
$bootstrap->bootstrap($application); | |
$r = $application->run(); | |
echo '</pre>'; | |
//-------------------------------------------------- | |
//$r->send(); |
This file contains hidden or 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
<h1>An error occurred</h1> | |
<h2><?php echo $this->message ?></h2> | |
<?php if (isset($this->display_exceptions) && $this->display_exceptions): ?> | |
<?php if(isset($this->exception) && $this->exception instanceof Exception): ?> | |
<h3>Exception information:</h3> | |
<p> | |
<h4>Message:</h4> | |
<pre><?php echo $this->exception->getMessage() ?></pre> | |
<h4>Stack trace:</h4> | |
<pre><?php echo $this->exception->getTraceAsString() ?></pre> | |
</p> | |
<?php else: ?> | |
<h3>No Exception available</h3> | |
<?php endif ?> | |
<?php endif ?> |
This file contains hidden or 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
index index |
This file contains hidden or 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
<?php | |
namespace Application\Controller; | |
use Zend\Mvc\Controller\ActionController; | |
class IndexController extends ActionController { | |
public function indexAction() { | |
return array('foo' => 'bar'); | |
} | |
} | |
This file contains hidden or 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
hello world |
This file contains hidden or 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
<?php | |
namespace Application\View; | |
use ArrayAccess, | |
Zend\Di\Locator, | |
Zend\EventManager\EventCollection, | |
Zend\EventManager\ListenerAggregate, | |
Zend\EventManager\StaticEventCollection, | |
Zend\Http\PhpEnvironment\Response, | |
Zend\Mvc\Application, | |
Zend\Mvc\MvcEvent, | |
Zend\View\Renderer; | |
class Listener implements ListenerAggregate | |
{ | |
protected $layout; | |
protected $listeners = array(); | |
protected $staticListeners = array(); | |
protected $view; | |
protected $displayExceptions = false; | |
public function __construct(Renderer $renderer, $layout = 'layout.phtml') | |
{ | |
$this->view = $renderer; | |
$this->layout = $layout; | |
} | |
public function setDisplayExceptionsFlag($flag) | |
{ | |
$this->displayExceptions = (bool) $flag; | |
return $this; | |
} | |
public function displayExceptions() | |
{ | |
return $this->displayExceptions; | |
} | |
public function attach(EventCollection $events) | |
{ | |
$this->listeners[] = $events->attach('dispatch.error', array($this, 'renderError')); | |
$this->listeners[] = $events->attach('dispatch', array($this, 'render404'), -1000); | |
$this->listeners[] = $events->attach('dispatch', array($this, 'renderLayout'), -80); | |
} | |
public function detach(EventCollection $events) | |
{ | |
foreach ($this->listeners as $key => $listener) { | |
$events->detach($listener); | |
unset($this->listeners[$key]); | |
unset($listener); | |
} | |
} | |
public function registerStaticListeners(StaticEventCollection $events, $locator) | |
{ | |
$ident = 'Zend\Mvc\Controller\ActionController'; | |
$handler = $events->attach($ident, 'dispatch', array($this, 'renderView'), -50); | |
$this->staticListeners[] = array($ident, $handler); | |
} | |
public function detachStaticListeners(StaticEventCollection $events) | |
{ | |
foreach ($this->staticListeners as $i => $info) { | |
list($id, $handler) = $info; | |
$events->detach($id, $handler); | |
unset($this->staticListeners[$i]); | |
} | |
} | |
public function renderView(MvcEvent $e) | |
{ | |
$response = $e->getResponse(); | |
if (!$response->isSuccess()) { | |
return; | |
} | |
$routeMatch = $e->getRouteMatch(); | |
$controller = $routeMatch->getParam('controller', 'index'); | |
$action = $routeMatch->getParam('action', 'index'); | |
$script = $controller . '/' . $action . '.phtml'; | |
$vars = $e->getResult(); | |
if (is_scalar($vars)) { | |
$vars = array('content' => $vars); | |
} elseif (is_object($vars) && !$vars instanceof ArrayAccess) { | |
$vars = (array) $vars; | |
} | |
$content = $this->view->render($script, $vars); | |
$e->setParam('content', $content); | |
return $content; | |
} | |
public function renderLayout(MvcEvent $e) | |
{ | |
$response = $e->getResponse(); | |
if (!$response) { | |
$response = new Response(); | |
$e->setResponse($response); | |
} | |
if ($response->isRedirect()) { | |
return $response; | |
} | |
$vars = $e->getResult(); | |
if (is_scalar($vars)) { | |
$vars = array('content' => $vars); | |
} elseif (is_object($vars) && !$vars instanceof ArrayAccess) { | |
$vars = (array) $vars; | |
} | |
if (false !== ($contentParam = $e->getParam('content', false))) { | |
$vars['content'] = $contentParam; | |
} | |
$layout = $this->view->render($this->layout, $vars); | |
$response->setContent($layout); | |
return $response; | |
} | |
public function render404(MvcEvent $e) | |
{ | |
$vars = $e->getResult(); | |
if ($vars instanceof Response) { | |
return; | |
} | |
$response = $e->getResponse(); | |
if ($response->getStatusCode() != 404) { | |
// Only handle 404's | |
return; | |
} | |
$vars = array( | |
'message' => 'Page not found.', | |
'exception' => $e->getParam('exception'), | |
'display_exceptions' => $this->displayExceptions(), | |
); | |
$content = $this->view->render('error/404.phtml', $vars); | |
$e->setResult($content); | |
return $this->renderLayout($e); | |
} | |
public function renderError(MvcEvent $e) | |
{ | |
$error = $e->getError(); | |
$response = $e->getResponse(); | |
if (!$response) { | |
$response = new Response(); | |
$e->setResponse($response); | |
} | |
switch ($error) { | |
case Application::ERROR_CONTROLLER_NOT_FOUND: | |
case Application::ERROR_CONTROLLER_INVALID: | |
$vars = array( | |
'message' => 'Page not found.', | |
'exception' => $e->getParam('exception'), | |
'display_exceptions' => $this->displayExceptions(), | |
); | |
$response->setStatusCode(404); | |
break; | |
case Application::ERROR_EXCEPTION: | |
default: | |
$vars = array( | |
'message' => 'An error occurred during execution; please try again later.', | |
'exception' => $e->getParam('exception'), | |
'display_exceptions' => $this->displayExceptions(), | |
); | |
$response->setStatusCode(500); | |
break; | |
} | |
$content = $this->view->render('error/index.phtml', $vars); | |
$e->setResult($content); | |
return $this->renderLayout($e); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Application; | |
use Zend\Module\Consumer\AutoloaderProvider; | |
class Module implements AutoloaderProvider { | |
public function getConfig() { | |
return array( | |
'layout' => 'layout/layout.phtml', | |
'di' => array( | |
'instance' => array( | |
'alias' => array( | |
'index' => 'Application\Controller\IndexController', | |
//'view' => 'Zend\View\PhpRenderer', | |
), | |
/* | |
'Zend\View\PhpRenderer' => array( | |
'parameters' => array( | |
'resolver' => 'Zend\View\TemplatePathStack', | |
'options' => array( | |
'script_paths' => array('application' => __DIR__ . '/views'), | |
), | |
) | |
), | |
*/ | |
) | |
), | |
'routes' => array( | |
'home' => array( | |
'type' => 'Zend\Mvc\Router\Http\Literal', | |
'options' => array( | |
'route' => '/', | |
'defaults' => array( | |
'controller' => 'index', | |
'action' => 'index' | |
) | |
) | |
) | |
) | |
); | |
} | |
public function getAutoloaderConfig() { | |
return array( | |
'Zend\Loader\StandardAutoloader' => array( | |
'namespaces' => array( | |
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__ | |
) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment