Loader of the Zend framework tutorial and Pluginloader usage, Zendpluginloader
This paper analyzes the usage of loader and Pluginloader in the Zend Framework. Share to everyone for your reference, as follows:
The Zend framework provides zend_loader for dynamically loading files.
Here's how it's used, and how it's implemented:
1. Loading files
How to use:
Zend_loader::loadfile ($filename, $dirs =null, $once =false);
Specific implementation:
/** * Loads a PHP file. This was a wrapper for PHP's include () function. * * $filename must is the complete filename and including any * extension such as ". php". Note that a security check is performed this * does not permit extended characters in the filename. This method was * intended for loading Zend Framework files. * If $dirs is a string or an array, it'll search the directories * in the order supplied, and attempt to load the Firs T matching file. * If the file is not found in the $dirs, or if no $dirs were specified, * It'll attempt to load it from PHP's include _path. * * If $once is TRUE, it would use include_once () instead of include (). * * @param string $filename * @param string|array $dirs-optional either a path or array of paths * to Sear Ch. * @param boolean $once * @return Boolean * @throws zend_exception */public static function LoadFile ($filename, $dir s = null, $once = False) {Self::_securitycheck ($filename); /** * Search in provided DirectorieS, as well as include_path */$incPath = false; if (!empty ($dirs) && (Is_array ($dirs) | | is_string ($dirs)) {if (Is_array ($dirs)) {$dirs = implode (path_ SEPARATOR, $dirs); } $incPath = Get_include_path (); Set_include_path ($dirs. Path_separator. $incPath); }/** * Try finding for the plain filename in the include_path. */if ($once) {include_once $filename; } else {include $filename; }/** * If searching in directories, reset Include_path */if ($incPath) {Set_include_path ($incPath); } return true;
Parameter rules:
As the implementation method, there are the following parameters
$filename parameter specifies the file to be loaded, note that $filename does not need to specify any path, only the file name is required. ZF will check the file for security. $filename can only be by letter, number, connector-, underline _ and English. Composition (half angle). $dirs parameters are not limited, you can use Chinese and so on.
The $dirs parameter is used to specify the directory in which the file resides, either as a string or as an array. If NULL, the program will go to the include_path of the system to find out if the file exists (Include_path can set the--haohappy note in php.ini), if it is a string or an array, it will go to the specified directory, and then the Include_ Path
The $once parameter is a Boolean type, or PHP function»include () if the file is loaded with PHP function»include_once () for True,zend_loader::loadfile (). (This parameter can only be true or false, the difference is the same as include () and include_once (). )
2. Load Class
Specific use:
Zend_loader::loadclass (' Container_tree ', Array ( '/home/production/mylib ', '/home/production/myapp ' ));
Specific implementation:
/*** Loads A class from a PHP file. The filename must be formatted* as "$class. php". * * If $dirs is a string or an array, it'll search the directories* in th E order supplied, and attempt to load the first matching file.** If $dirs is null, it would split the class name at UNDERSC Ores to* Generate a path hierarchy (e.g., "Zend_example_class" would map* to "zend/example/class.php"). * * If the file was n OT found in the $dirs, or if no $dirs were specified,* it would attempt to load it from PHP ' s include_path.** @param string $class-the Full class name of a Zend component.* @param string|array $dirs-optional either a path or an array of PA ths* to search.* @return void* @throws zend_exception*/public static function LoadClass ($class, $dirs = null {if (Class_exists ($class, false) | | interface_exists ($CLASS, False)) {return; } if ((null!== $dirs) &&!is_string ($dirs) &&!is_array ($dirs)) {require_once ' zend/exception.ph P '; throw new Zend_exception (' Directory argument must is a string or an array '); }//Autodiscover the path from the class name//implementation are PHP Namespace-aware, and based on//Framewor K Interop Group Reference implementation://Http://groups.google.com/group/php-standards/web/psr-0-final-proposal $ ClassName = LTrim ($class, ' \ \ '); $file = "; $namespace = "; if ($lastNsPos = Strripos ($className, ' \ \ ')) {$namespace = substr ($className, 0, $lastNsPos); $className = substr ($className, $lastNsPos + 1); $file = str_replace (' \ \ ', Directory_separator, $namespace). Directory_separator; } $file. = Str_replace (' _ ', Directory_separator, $className). '. php '; if (!empty ($dirs)) {//Use the autodiscovered path $dirPath = dirname ($file); if (is_string ($dirs)) {$dirs = explode (Path_separator, $dirs); } foreach ($dirs as $key = + $dir) {if ($dir = = '. ') {$dirs [$key] = $dirPath; } else { $dir = RTrim ($dir, ' \\/'); $dirs [$key] = $dir. Directory_separator. $dirPath; }} $file = BaseName ($file); Self::loadfile ($file, $dirs, true); } else {self::loadfile ($file, NULL, TRUE); } if (!class_exists ($class, False) &&!interface_exists ($class, False)) {require_once ' zend/exception.php '; throw new Zend_exception ("File \" $file \ "does not exist or class \" $class \ "is not found in the File"); }}
$class class name will be based on the underscore (as a directory separator line) corresponding to the PHP file in the corresponding directory, and add '. php ', such as Container_tree will point to container\\tree.php.
$dir can be an array or a string. The directory is the path to the directory that the class name contains.
3. Determine if a file is readable
Specific use:
if (zend_loader::isreadable ($filename)) { //do something with $filename}
Specific implementation:
/** * Returns TRUE if the $filename is readable, or FALSE otherwise. * This function uses the PHP include_path, where PHP's is_readable () * does not. * * Note from ZF-2900: * If Your use of custom error handler, please check whether return value * from error_reporting () is ze Ro or not. * At Mark of fopen () can not suppress warning if the handler is used. * * @param string $filename * @return Boolean */public static function IsReadable ($filename) {if (Is_readable ($filename) {//return early if the filename is readable without needing the//Include_path return true; } if (Strtoupper (substr (php_os, 0, 3)) = = ' WIN ' && preg_match ('/^[a-z]:/i ', $filename)) {//If on window s, and path provided is clearly a absolute path,//return false immediately return false; } foreach (Self::explodeincludepath () as $path) {if ($path = = '. ') {if (is_readable ($filename)) {return true; } continue; } $file = $path. '/' . $filename; If (Is_readable ($file)) {return true; }} return false;}
Specific parameters:
The $filename parameter specifies the name of the file to check, including the path information. This method encapsulates the PHP function»is_readable (), and is_readable () does not automatically find the file under Include_path, and zend::isreadable () can.
4.Autoloader
The autoloader function of this class is deprecated, so it is no longer described. There are other autoloader, specify later.
5. Plugin Loader
The specific examples given in the Help article are as follows, which can be referred to using:
Many Zend Framework components support plug-ins that allow dynamic loading of functions by specifying the prefix of the class and the path to the class's file (which does not need to be in include_path or files that do not need to follow the traditional naming convention). Zend_loader_pluginloader provides a common function to accomplish this work.
The basic usage of Pluginloader follows the naming convention of the Zend Framework (a class of files), when parsing a path, using underscores as the path delimiter. When deciding whether to load a particular plug-in class, it is possible to pass the optional class prefix to preprocess. In addition, the paths are searched in LIFO order. Because of the prefix of the LIFO search and the class, the namespace is allowed to the plug-in, which overwrites the plugin from the earlier registered path.
Basic Use Cases
First, assume the following directory structure and class file, and the root (toplevel) directory and library directory are in Include_path:
application/
modules/
foo/
views/
helpers/
formlabel.php
formsubmit.php
bar/
views/
helpers/
formsubmit.php
library/
zend/
view/
helper/
formlabel.php
formsubmit.php
formtext.php
Now create a plug-in loader to make a variety of view assistant warehouses available:
<?php$loader = new Zend_loader_pluginloader (); $loader->addprefixpath (' Zend_view_helper ', ' zend/view/helper/' ) ->addprefixpath (' Foo_view_helper ', ' application/modules/foo/views/helpers ') ->addprefixpath (' Bar_view_helper ', ' application/modules/bar/views/helpers ');? >
A given view helper is then loaded with the portion of the prefix defined after the path is added to the class name:
<?php//load ' FormText ' helper: $formTextClass = $loader->load (' FormText '); ' Zend_view_helper_formtext ';//Load ' Formlabel ' Helper: $formLabelClass = $loader->load (' Formlabel '); ' Foo_view_helper_formlabel '//Load ' Formsubmit ' Helper: $formSubmitClass = $loader->load (' Formsubmit '); ' Bar_view_helper_formsubmit '?>
After the class is loaded, it can be instantiated.
Note: Registering multiple paths for a prefix
Sometimes, multiple paths use the same prefix, Zend_loader_pluginloader actually registers an array of paths for each given prefix, and the last sign is checked first, which is useful when you use the components in the incubator.
Note: Define a path when instantiating
You can give the constructor an optional "prefix/path" pair (or "prefix/multiple path") array parameters:
<?php$loader = new Zend_loader_pluginloader (Array ( ' zend_view_helper ' = ' zend/view/helper/', ' Foo_ View_helper ' = ' application/modules/foo/views/helpers ', ' bar_view_helper ' = ' application/modules/bar/ Views/helpers '));? >
Zend_loader_pluginloader allows the sharing of plug-ins without the need to use a singleton instance, which is done through a static registry, which requires a registry name as the second parameter of the constructor when instantiated:
<?php//Store plugins in static registry ' Foobar ': $loader = new Zend_loader_pluginloader (Array (), ' foobar ');? >
Other components that instantiate the Pluginloader with the same name registry will have access to the loaded paths and plugins.
Working with plug-in paths
Example of the previous section how to add a path to the plug-in loader, so how do you determine which paths have been loaded or delete them?
If no $prefix is provided, getpaths ($prefix = null) returns all paths with a "prefix/path" pair, or if $prefix is provided, getpaths ($prefix = null) returns the path registered for the given prefix.
Clearpaths ($prefix = null) clears all registered paths by default, or if a $prefix is provided and placed on the stack, only the paths associated with the given prefix are cleared.
Removeprefixpath ($prefix, $path = null) allows you to selectively purge specific paths associated with a given prefix. If no $path is provided, all the prefix-related paths are cleared, and if the $path is provided and the corresponding prefix exists, only the associated path is cleared.
Test the plug-in and get the name of the class
Sometimes you want to determine whether the plug-in class has loaded before executing an action, and isLoaded () returns the status of the plug-in name.
Another common use case for Pluginloader is to determine the fully qualified plug-in class name for the loaded class, and GetClassName () provides that functionality. Generally, this is used in conjunction with isLoaded ():
<?phpif ($loader->isloaded (' Adapter ')) { $class = $loader->getclassname (' Adapter '); $adapter = Call_user_func (Array ($class, ' getinstance '));}? >
The implementation of the specific plug-in loader can refer to Zend_loader_pluginloader and Zend_loader. This is not a statement of exhaustion.
More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- Zend Framework Tutorial configuration file Application.ini parsing
- Zend Framework Tutorial Autoloading Usage
- Examples of resource autoloading usages of the Zend framework tutorial
- Controller usage Analysis of MVC Framework for Zend Framework Tutorial
- Zend Framework Tutorial's routing function zend_controller_router detailed
- Zend Framework Tutorial Zend_controller_plugin plugin Usage
- Package Zend_controller_response Example of response object for Zend Framework tutorial
- Package Zend_controller_request example of request object for Zend Framework tutorial
- Zend Framework Tutorial Action base class Zend_controller_action detailed
- Zend Framework Tutorial Distributor Zend_controller_dispatcher Usage
- Zend Framework Tutorial Front Controller Zend_controller_front Usage
- Zend Framework Tutorial application and bootstrap usage
http://www.bkjia.com/PHPjc/1108615.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108615.html techarticle Zend Framework Tutorial Loader and Pluginloader usage, Zendpluginloader This example analyzes Zend and loader usage in the Pluginloader framework. Share to everyone for your reference, ...