Getting an
Additionally, you will often want to reuse the same code tobootstrap your tests, a cronjob, or a service script. While it'spossible to simply include your bootstrap script, oftentimes thereare initializations that are environment specific – you may notneed the
Zend_Application
Zend_Application
-
Zend_Application: loads the
PHP environment,including include_paths and autoloading, and instantiates therequested bootstrap class. -
Zend_Application_Bootstrap: provides interfaces forbootstrap classes.Zend_Application_Bootstrap_Bootstrap
providescommon functionality for most bootstrapping needs, includingdependency checking algorithms and the ability to load bootstrapresources on demand. -
Zend_Application_Resource
provides aninterface for standard bootstrapping resources that can be loadedon demand by a bootstrap instance, as well as several defaultresource implementations.
Developers create a bootstrap class for their application,extendingZend_Application_Bootstrap_Bootstrap
-
The current environment
-
Options for bootstrapping
The bootstrap options include the path to the file containing thebootstrap class and optionally:
-
Any extra include_paths to set
-
Any additional autoloader namespaces to register
-
Any
php.ini settings toinitialize -
The class name for the bootstrap class (if not "Bootstrap")
-
Resource prefix to path pairs to use
-
Any resources to use (by class name or short name)
-
Additional path to a configuration file to load
-
Additional configuration options
Options may be an array, a
Bootstrapping ¶
Zend_Application's second area of responsibility isexecuting the application bootstrap. Bootstraps minimally need toimplement
-
interface
Zend_Application_Bootstrap_Bootstrapper -
{
-
public function __construct ( $application ); -
public function getApplication ( ); -
public function getEnvironment ( ); -
public function getClassResources ( ); -
public function getClassResourceNames ( ); -
public function bootstrap ( $resource = null ); -
public function run ( ); -
}
This
You can implement this interface on your own, extendZend_Application_Bootstrap_BootstrapAbstract,or useZend_Application_Bootstrap_Bootstrap.
Besides this functionality, there are a number of other areas ofconcern you should familiarize yourself with.
Resource Methods ¶
The
To bootstrap a single resource method, use the
To bootstrap several resource methods, pass an array of names. Toobootstrap all resource methods, pass nothing.
Take the following bootstrap class:
-
class
Bootstrap extends Zend_Application_Bootstrap_Bootstrap -
{
-
protected function _initFoo ( ) -
{ -
//... -
} -
-
protected function _initBar ( ) -
{ -
//... -
} -
-
protected function _initBaz ( ) -
{ -
//... -
} -
}
To bootstrap just the
-
$bootstrap-> bootstrap ( 'foo' );
To bootstrap the
To bootstrap all resource methods, call
-
$bootstrap-> bootstrap ( );
Bootstraps that use resource plugins ¶
To make your bootstraps more re-usable, we have provided theability to push your resources into resource plugin classes. Thisallows you to mix and match resources simply via configuration. Wewill cover
If your bootstrap should be capable of using resource plugins, youwill need to implement an additional interface,
-
interface
Zend_Application_Bootstrap_ResourceBootstrapper -
{
-
public function registerPluginResource ( $resource, $options = null ); -
public function unregisterPluginResource ( $resource ); -
public function hasPluginResource ( $resource ); -
public function getPluginResource ( $resource ); -
public function getPluginResources ( ); -
public function getPluginResourceNames ( ); -
public function setPluginLoader (Zend_Loader_PluginLoader_Interface $loader ); -
public function getPluginLoader ( ); -
}
Resource plugins basically provide the ability to create resourceintializers that can be re-used between applications. This allowsyou to keep your actual bootstrap relatively clean, and tointroduce new resources without needing to touch your bootstrapitself.
Zend_Application_Bootstrap_BootstrapAbstract
To utilize resource plugins, you must specify them in the optionspassed to the application object and/or bootstrap. These optionsmay come from a configuration file, or be passed in manually.Options will be of key to options pairs, with the key representingthe resource name. The resource name will be the segment followingthe class prefix. For example, the resources shipped with ZendFramework have the class prefix "Zend_Application_Resource_";anything following this would be the name of the resource. As anexample,
This indicates that the "FrontController" resource should be used,with the options specified.
If you begin writing your own resource plugins, or utilizethird-party resource plugins, you will need to tell your bootstrapwhere to look for them. Internally, the bootstraputilizesZend_Loader_PluginLoader,so you will only need to indicate the common class prefix an pathpairs.
As an example, let's assume you have custom resource pluginsin
You would now be able to use resources from that directory.
Just like resource methods, you use the
-
//Execute one:
-
$bootstrap-> bootstrap ( 'FrontController' );
-
-
//Execute several:
-
-
//Execute all resource methods and plugins:
-
$bootstrap-> bootstrap ( );
Resource Registry ¶
Many, if not all, of your resource methods or plugins willinitialize objects, and in many cases, these objects will be neededelsewhere in your application. How can you access them?
Zend_Application_Bootstrap_BootstrapAbstract
For maximum flexibility, this registry is referred to as a"container" internally; its only requirements are that it is anobject. Resources are then registered as properties named after theresource name. By default, an instance of
As an example, consider a basic view resource:
-
class
Bootstrap extends Zend_Application_Bootstrap_Bootstrap -
{
-
protected function _initView ( ) -
{ -
$view = new Zend_View ( ); -
// moreinitialization... -
-
return $view; -
} -
}
You can then check for it and/or fetch it as follows:
-
//Using the has/getResource() pair:
-
if
( $bootstrap-> hasResource ( 'view' ) ) { -
$view = $bootstrap-> getResource ( 'view' ); -
}
-
-
// Viathe container:
-
$container
= $bootstrap-> getContainer ( ); -
$view = $container-> view; -
}
Please note that the registry and also the container is not global.This means that you need access to the bootstrap in order to fetchresources.
As an example, if you wanted access to the view resource from abovewithin your action controller, you could do the following:
-
class
FooController extends Zend_Controller_Action -
{
-
public function init ( ) -
{ -
$bootstrap = $this-> getInvokeArg ( 'bootstrap' ); -
$view = $bootstrap-> getResource ( 'view' ); -
//... -
} -
}
Dependency Tracking ¶
In addition to executing resource methods and plugins, it'snecessary to ensure that these are executed once and once only;these are meant to bootstrap an application, and executing multipletimes can lead to resource overhead.
At the same time, some resources may depend on other resourcesbeing executed. To solve these two issues,
As noted previously, all resources -- whether methods or plugins --are bootstrapped by callingbootstrap($resource),where
If a resource depends on another resource, it shouldcall
In a resource method, such a call would look like this:
-
class
Bootstrap extends Zend_Application_Bootstrap_Bootstrap -
{
-
protected function _initRequest ( ) -
{ -
// Ensure thefront controller is initialized -
$this-> bootstrap ( 'FrontController' ); -
-
// Retrieve thefront controller from the bootstrap registry -
$front = $this-> getResource ( 'FrontController' ); -
-
$request = new Zend_Controller_Request_Http ( ); -
$request-> setBaseUrl ( '/foo' ); -
$front-> setRequest ( $request ); -
-
// Ensure therequest is stored in the bootstrap registry -
return $request; -
} -
}
Resource Plugins ¶
As noted previously, a good way to create re-usable bootstrapresources and to offload much of your coding to discrete classes isto utilize resource plugins. While Zend Framework ships with anumber of standard resource plugins, the intention is thatdevelopers should write their own to encapsulate their owninitialization needs.
Resource plugins need only implement
-
interface
Zend_Application_Resource_Resource -
{
-
public function __construct ( $options = null ); -
public function setBootstrap ( -
Zend_Application_Bootstrap_Bootstrapper $bootstrap -
); -
public function getBootstrap ( ); -
public function getOptions ( ); -
public function init ( ); -
}
The interface defines simply that a resource plugin should acceptoptions to the constructor, have mechanisms for setting andretrieving options, have mechanisms for setting and retrieving thebootstrap object, and an initialization method.
As an example, let's assume you have a common view intializationyou use in your applications. You have a commondoctype,
-
class
My_Resource_View extendsZend_Application_Resource_ResourceAbstract -
{
-
protected $_view; -
-
public function init ( ) -
{ -
// Return view sobootstrap will store it in the registry -
return $this-> getView ( ); -
} -
-
public function getView ( ) -
{ -
if ( null === $this->_view ) { -
$options = $this-> getOptions ( ); -
$title = ''; -
$title = $options [ 'title' ]; -
} -
-
$view = new Zend_View ( $options ); -
$view-> doctype ( 'XHTML1_STRICT' ); -
$view-> headTitle ( $title ); -
$view-> headLink ( )-> appendStylesheet ( '/css/site.css' ); -
$view-> headScript ( )-> appendfile ( '/js/analytics.js' ); -
-
$viewRenderer = -
Zend_Controller_Action_HelperBroker:: getStaticHelper ( -
'ViewRenderer' -
); -
$viewRenderer-> setView ( $view ); -
-
$this->_view= $view; -
} -
return $this->_view; -
} -
}
As long as you register the prefix path for this resource plugin,you can then use it in your application. Even better, because ituses the plugin loader, you are effectively overriding the shipped"View" resource plugin, ensuring that your own is used instead.