View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}

{zone-data:component-name}
Zend_Loader_Autoloader_Resource
{zone-data}

{zone-data:proposer-list}
[~matthew]
{zone-data}

{zone-data:liaison}
[~ralph]
{zone-data}

{zone-data:revision}
1.0 - 1 January 2008: Initial Draft.
{zone-data}

{zone-data:overview}
Zend_Loader_Autoloader_Resource is intended to simplify loading resources that do not have 1:1 class prefix/filesystem mappings. The primary use case is for use with modular applications, to allow autoloading of module-specific classes.
{zone-data}

{zone-data:references}
* [Pastebin resource loader implementation|http://github.com/weierophinney/pastebin/tree/bugapp/library/My/Loader/Autoloader/Resource.php]
* [Zend_Controller_Action_Helper_ResourceLoader proposal|http://framework.zend.com/wiki/display/ZFPROP/Zend_Controller_Action_Helper_ResourceLoader+-+Graham+Anderson]
{zone-data}

{zone-data:requirements}
* This component *will* implement Zend_Loader_Autoloader_Interface.
* This component *will* require specification of both a prefix and base directory
* This component *will* provide default component/subdirectory mappings
** Users *will* be able to register custom component/subdirectory mappings
** This component *will* allow autoloading arbitrarily nested classes under existing component maps
* This component *should* also act as an object factory and registry for the given resource namespace
{zone-data}

{zone-data:dependencies}
* Zend_Loader_Exception
* [Zend_Loader_Autoloader|ZFPROP:Zend_Loader_Autoloader - Ralph Schindler]
{zone-data}

{zone-data:operation}
Typically, this component will be instantiated in a module's bootstrap. It's purpose is to provide the ability to autoload components in that subdirectory, assuming a standard directory layout.

For instance, the bootstrap might do the following:
{code:php}
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'prefix' => 'Spindle',
'basePath' => realpath(dirname(__FILE__)),
));
Zend_Loader_Autoloader::pushAutoloader($autoloader);
{code}

This would then allow your module code to use autoloading to arbitrary resources. For instance, the following directory structure would establish the associated classes:
{code}
application/
modules/
spindle/
models/
Bug.php // Spindle_Model_Bug
forms/
Bug.php // Spindle_Form_Bug
{code}
When the resource autoloader is in play, you can simply instantiate these classes, without worrying where they are:
{code:php}
$form = new Spindle_Form_Bug();
{code}
{zone-data}

{zone-data:milestones}
* Milestone 1: \[DONE\] Proposal created
* Milestone 2: \[DONE\] Working prototype checked into public repository
* Milestone 3: Community review of proposal
* Milestone 4: Proposal acceptance
* Milestone 5: Passing unit tests and initial documentation committed to incubator
* Milestone 6: Review for inclusion in trunk
{zone-data}

{zone-data:class-list}
* Zend_Loader_Autoloader_Resource
{zone-data}

{zone-data:use-cases}
||UC-01||
Basic usage:
{code:php}
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'prefix' => 'Spindle',
'basePath' => realpath(dirname(__FILE__)),
));
Zend_Loader_Autoloader::pushAutoloader($resourceLoader);
{code}

||UC-02||
Using as an object factory and registry:
{code:php}
/* Spindle_Model_Bug */
$bugModel = $resourceLoader->getModel('bug');
{code}

||UC-03||
Registering custom resource paths:
{code:php}
// Adds 'validators' resource, mapping to basePath + /validators, using prefix
// $prefix _Validate.
$resourceLoader->addResourceType('validator', 'validators', 'Validate');

// e.g., this loads the class from
// application/modules/spindle/validators/Foo.php
$foo = new Spindle_Validate_Foo();
{code}
{zone-data}

{zone-data:skeletons}
{code:php}
class Zend_Loader_Autoloader_Resource implements Zend_Loader_Autoloader_Interface
{
public function __construct($options);

public function initDefaultResourceTypes();

public function __call($method, $args);

public function autoload($class);

public function setOptions(array $options);

public function setPrefix($prefix);

public function getPrefix();

public function setBasePath($path);

public function getBasePath();

public function addResourceType($type, $path, $prefix = null);

public function addResourceTypes(array $types);

public function setResourceTypes(array $types);

public function getResourceTypes();

public function hasResourceType($type);

public function removeResourceType($type);

public function clearResourceTypes();

public function setDefaultResourceType($type);

public function getDefaultResourceType();

public function load($resource, $type = null);
}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>