Skip to content

Instantly share code, notes, and snippets.

@drslump
Forked from pepe84/App.php
Created July 5, 2011 11:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drslump/1064680 to your computer and use it in GitHub Desktop.
Save drslump/1064680 to your computer and use it in GitHub Desktop.
PHP Zend: Resources lazy loading
[production]
; PHP INI Settings
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
; Define include paths for the application
includepaths[] = APPLICATION_PATH
includepaths[] = APPLICATION_PATH "/models"
includepaths[] = APPLICATION_PATH "/models/mappers"
; Namespaces for autoload
appnamespace = ""
; Autoloader Options
autoloaderNamespaces[] = "My"
autoloaderNamespaces[] = "PHPTAL"
; Bootstrap Location
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
; Plugins
pluginPaths.My_Zend_Application_Resource = APPLICATION_ROOT "/library/My/Zend/Application/Resource"
; All resources used by Zend_Application are loaded on every request
autoloadResources[] = 'frontController'
autoloadResources[] = 'session'
autoloadResources[] = 'view'
autoloadResources[] = 'layout'
autoloadResources[] = 'db'
; Symfony ServiceContainer using SfBootstrap
;container = "My_Symfony_sfServiceContainer"
...
<?php
class My_Zend_Application_Bootstrap_LazyBootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Bootstrap implementation
*
* This method may be overridden to provide custom bootstrapping logic.
* It is the sole method called by {@link bootstrap()}.
*
* @param null|string|array $resource
* @return void
* @throws Zend_Application_Bootstrap_Exception When invalid argument was passed
*/
protected function _bootstrap($resource = null)
{
if (null === $resource && $this->hasOption('autoloadResources')) {
// Bootstrap all? No, lazy loading...
$resource = $this->getOption('autoloadResources');
}
parent::_bootstrap($resource);
}
/**
* Execute a resource and apply custom configurations
*
* @param string $resource
* @return void
* @throws Zend_Application_Bootstrap_Exception When resource not found
*/
protected function _executeResource($resource)
{
$resourceLower = strtolower($resource);
// Check if it haven't been yet initialized
if (!in_array($resourceLower, $this->_run)) {
// Setup resource in the container
parent::_executeResource($resource);
// Check if we have a config method for this resource
$configMethod = '_config' . $resource;
if (method_exists($this, $configMethod)) {
// Get resource from container
$object = parent::getResource($resource);
// Flag resource to avoid circular dependencies
$this->_started[$resourceLower] = true;
// Configure resource
if ($override = $this->$configMethod($object)) {
$this->setResource($resource, $override);
}
// Unflag circular protection
unset($this->_started[$resourceLower]);
}
}
}
/**
* Retrieve a resource from the container
*
* @param string $name
* @return null|mixed
*/
public function getResource($name)
{
// Make sure the resource is bootstraped
if (!$this->hasResource($name)) {
$this->bootstrap($name);
}
return parent::getResource($name);
}
/**
* Set a resource into the container
*
* @param string $name
* @param object $object
*/
public function setResource($name, $object)
{
$resource = strtolower($name);
$container = $this->getContainer();
if ($this->hasResource($resource)) {
$container->{$resource} = $object;
}
}
}
<?php
class My_Zend_Application_Bootstrap_SfBootstrap extends My_Zend_Application_Bootstrap_LazyBootstrap
{
/**
* Sets resource container
*
* We are extending this method so we can use Symfony's dependency injection
* container instead of Zend_Registry. If the $container argument is a
* string it's evaluated as the path to a php file defining the container.
* If the argument is an object the default parent behaviour is used.
*
* @param string|object $container
* @return My_Zend_Application_Bootstrap_SfBootstrap (fluent interface)
*/
public function setContainer($container)
{
if (is_string($container)) {
// Setup Symfony's DI autoloader
include_once 'Symfony/DependencyInjection/sfServiceContainerAutoloader.php';
sfServiceContainerAutoloader::register();
// Convert tree-like config structure to a hashmap (dot separated)
$params = $this->_flattenArray($this->getOption('resources'));
$container = new $container($params);
}
parent::setContainer($container);
return $this;
}
/**
* Flattens a multidimmensional array separating nesting levels with a dot
*
* @param array $params
* @param string $path
* @return array
*/
protected function _flattenArray($params, $path = '')
{
$keys = array_keys($params);
$values = array();
foreach ($keys as $k) {
$values[$path . $k] = $params[$k];
if (is_array($params[$k])) {
$arr = $this->_flattenArray($params[$k], $path . $k . '.');
$values = array_merge($values, $arr);
}
}
return $values;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment