Zendframework resource Loading Process Analysis

Source: Internet
Author: User
Tags autoloader

This article mainly analyzes the loading process of custom resources in zendframework1.11, and also sorts out problems encountered by individuals during the analysis.
1. To understand this process, we first configure the product from the existing resource development instance (correct example) on the Internet.
Add the Resource Directory under the library directory as the path for personal development resource plug-in;
Add a view plug-in to replace the initview method under the existing Bootstrap. php.
<? PHP
Class resource_view extends zend_application_resource_resourceabstract
{
Protected $ _ view;
 
Public Function Init ()
{
// Return view so Bootstrap will store it in the Registry
Return $ this-> getview ();
}
 
Public Function getview ()
{
If (null ===$ this-> _ view ){
$ Options = $ this-> getoptions ();
$ Title = '';
If (array_key_exists ('title', $ options )){
$ Title = $ options ['title'];
Unset ($ 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 ;}
}
In this way, we have a resource. In the next step, we need to configure Zend to load it.
I personally like to use PHP arrays to configure Zend (zf2.0 also uses Arrays :))
<? PHP
// Product
$ Phpsettings ['display _ startup_errors '] = 0;
$ Phpsettings ['display _ errors '] = 0;
$ Bootstrap ['path'] = application_path. "/Bootstrap. php ";
$ Bootstrap ['class'] = "Bootstrap ";
$ Resources ['frontcontroller'] ['controllerdirectory'] = application_path. "/controllers ";
$ Resources ['frontcontroller'] ['params'] ['displayexception'] = 0;
$ Resources ['view'] = true;
$ Autoloadernamespaces = "resource _";
$ Pluginpaths ['resource'] = "resource ";

$ Configs ['autoloadernamespaces '] [] = $ autoloadernamespaces;
$ Configs ['ininpaths '] = $ pluginpaths;
$ Configs ['phpsetsecret'] = $ phpsettings;
$ Configs ['includepaths '] = $ includepaths;
$ Configs ['bootstrap '] = $ Bootstrap;
$ Configs ['appnamespace'] = "application ";
$ Configs ['resources'] = $ resources;
$ Configs ['path'] = application_path. "/views/scripts ";
The view in the resource array is added to add our custom view resources;
Autoloadernamespaces increases the resource namespace, which involves the automatic file loading of zf1;
Pluginpaths adds the plug-in path for custom resources, that is, the path under include_path (I personally feel redundant)

2. zf1 initialization and resource Loading
This part is mainly implemented in the application loading process in the index. php entry file. Let's take a look at this process.
$ Application = new zend_application (
Application_env,
$ Configs );
$ Application-> Bootstrap ()
-> Run ();
Zend_application instantiation includes the instantiation of the class loader and setting configuration parameters.
Require_once 'zend/loader/autoloader. php ';
$ This-> _ autoloader = zend_loader_autoloader: getinstance ();
Setting parameters is actually to help developers automatically set phpsetting and includepath, automatically load namespaces (autoloadernamespaces), and instantiate the bootstrap class (this instantiation process loads frontcontroller resources, loaded in the class constructor of zend_application_bootstrap_bootstrap). For details, refer to the setoptions method.
Supplement: The pluginpaths parameter will be added to the setoptions method of zend_application_bootstrap_bootstrapabstract as the path parameter of the plug-in loader.
Autoloadernamespaces: sets the class prefix that can be loaded by the auto-loader. If you do not set a custom class namespace, the corresponding class cannot be loaded, this setting can be done in the configuration file (application. PHP | application. INI), you can also manually set it through $ loader-> registernamespace ($ namespace). For details, see the zend_loader_autoloader.php file.
$ Application's Bootstrap () method mainly loads resources, including required resources and custom Resources in the framework. The loading process is implemented in the _ Bootstrap Method of the zend_application_bootstrap_bootstrapabstract class.
Protected function _ Bootstrap ($ resource = NULL)
{
If (null ===$ Resource ){
Foreach ($ this-> getclassresourcenames () as $ Resource ){
$ This-> _ executeresource ($ Resource );
}

Foreach ($ this-> getpluginresourcenames () as $ Resource ){
$ This-> _ executeresource ($ Resource );
}
} Elseif (is_string ($ Resource )){
$ This-> _ executeresource ($ Resource );
} Elseif (is_array ($ Resource )){
Foreach ($ resource as $ R ){
$ This-> _ executeresource ($ R );
}
} Else {
Throw new zend_application_bootstrap_exception ('invalid argument passed to '. _ method __);
}
}
There are two main parts for processing
1. The loaded class-defined resources are basically the Resource Initialization behaviors defined by individuals through the _ init method during development. In this process, the class resources are obtained through the getclassresourcenames method, use the reflectionobject or get_class_methods method to obtain the class method name.
For example, you can define the following method in Bootstrap. php to automatically execute and initialize the request resource.
Protected function _ initrequest ()
{
// Ensure Front Controller instance is present, and fetch it
$ This-> Bootstrap ('frontcontroller ');
$ Front = $ this-> getresource ('frontcontroller ');
 
// Initialize the request object
$ Request = new zend_controller_request_http ();
$ Request-> setbaseurl ('/foo ');
// Add it to the Front Controller
$ Front-> setrequest ($ request );
 
// Bootstrap will store this value in the 'request' key of its container
Return $ request;
}
2. load custom class resources, load the resources set in the configuration, or load the parameter resources provided by the Bootstrap Method (because the resources have multiple settings loading problems, _ executeresource adds the judgment on whether the resource is loaded.) It is loaded by the _ Bootstrap Method of zend_application_bootstrap_bootstrapabstract.
Protected function _ Bootstrap ($ resource = NULL)
{
If (null ===$ Resource ){
Foreach ($ this-> getclassresourcenames () as $ Resource ){
$ This-> _ executeresource ($ Resource );
}

Foreach ($ this-> getpluginresourcenames () as $ Resource ){
$ This-> _ executeresource ($ Resource );
}
} Elseif (is_string ($ Resource )){
$ This-> _ executeresource ($ Resource );
} Elseif (is_array ($ Resource )){
Foreach ($ resource as $ R ){
$ This-> _ executeresource ($ R );
}
} Else {
Throw new zend_application_bootstrap_exception ('invalid argument passed to '. _ method __);
}
}
The loading process is executed in _ loadpluginresource. The load of zend_loader_pluginloader verifies the load Based on the Resource Name and returns the class name. instantiate and register the resource plug-in _ loadpluginresource of zend_application_bootstrapabstract.
Protected function _ loadpluginresource ($ resource, $ options)
{
$ Options = (array) $ options;
$ Options ['bootstrap '] = $ this;
$ Classname = $ this-> getpluginloader ()-> load (strtolower ($ resource), false );

If (! $ Classname ){
Return false;
}

$ Instance = new $ classname ($ options );

Unset ($ this-> _ pluginresources [$ resource]);
If (isset ($ instance-> _ explicittype )){
$ Resource = $ instance-> _ explicittype;
}
$ Resource = strtolower ($ Resource );
$ This-> _ pluginresources [$ resource] = $ instance;

Return $ resource;
}

The next step is to execute the init method of the resource in _ executeresource, and save the init return result in the _ Container (which is a zend_registry instance), so that the resource loading and cache are OK.

Refer:

Http://blog.madarco.net/327/how-to-make-your-own-zend-framework-resource-plugin/

Http://framework.zend.com/manual/1.11/en/zend.application.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.