Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Loader Class Reference

Enhances Zend_Loader to provide a package registry and auto-loading of 'special' module classes such as: controllers, forms, models, etc. More...

List of all members.

Static Public Member Functions

static addPackagePath ($namespace, $path)
 Register a module path to be checked when auto-loading classes.
static autoload ($class)
 Load classes via package registry and with knowledge of 'special' module classes such as: controllers, forms, models, etc.
static getPackagePaths ()
 Get the set of registered module paths.
static setPackagePaths ($paths)
 Set module paths to the given array of paths keyed on namespaces.

Static Protected Member Functions

static _resolveSpecialClass ($class, $package)
 Find the path to a special class that resides in a registered package.
static _securityCheck ($filename)
 Ensure that filename does not contain exploits.

Static Protected Attributes

static $_packages = array()

Detailed Description

Enhances Zend_Loader to provide a package registry and auto-loading of 'special' module classes such as: controllers, forms, models, etc.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

static P4Cms_Loader::_resolveSpecialClass ( class,
package 
) [static, protected]

Find the path to a special class that resides in a registered package.

Parameters:
string$classthe name of the class to resolve.
string$packagethe name of the package.
Returns:
false|string the path to the class, or false if unsuccessful.
    {
        // map various class types to their associated sub-folders.
        $classTypes = array(
            "Acl"               => "acls",
            "Acl_Assert"        => "acls/asserts",
            "Controller_Helper" => "controllers/helpers",
            "Filter"            => "filters",
            "Form"              => "forms",
            "Form_Decorator"    => "forms/decorators",
            "Form_Element"      => "forms/elements",
            "Model"             => "models",
            "Test"              => "tests",
            "Validate"          => "validators",
            "View_Helper"       => "views/helpers"
        );

        // extract class prefix (sans package name) and suffix.
        $suffix = substr($class, strrpos($class, '_') + 1);
        $prefix = substr($class,  strpos($class, '_') + 1, -(strlen($suffix) + 1));

        // generate path according to class type:
        //      controllers - special case ending in 'Controller'
        //  special classes - map class prefix to path
        if (!$prefix && substr($suffix, -10) === "Controller") {
            $path = "controllers/" . $suffix;
        } else if (isset($classTypes[$prefix])) {
            $path = $classTypes[$prefix] . '/' . $suffix;
        } else {
            return false;
        }

        // return filename.
        $file = static::$_packages[$package] . '/' . $path . ".php";
        if (is_readable($file)) {
            return $file;
        } else {
            return false;
        }
    }
static P4Cms_Loader::_securityCheck ( filename) [static, protected]

Ensure that filename does not contain exploits.

Parameters:
string$filenamethe filename to check for exploits
Returns:
void
Exceptions:
Zend_Exceptionif the filename contains illegal characters
    {
        if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
            require_once 'Zend/Exception.php';
            throw new Zend_Exception('Security check: Illegal character in filename');
        }
    }
static P4Cms_Loader::addPackagePath ( namespace,
path 
) [static]

Register a module path to be checked when auto-loading classes.

Parameters:
string$namespacethe module namespace.
string$paththe absolute path to the module.
    {
        self::$_packages[$namespace] = $path;
    }
static P4Cms_Loader::autoload ( class) [static]

Load classes via package registry and with knowledge of 'special' module classes such as: controllers, forms, models, etc.

Parameters:
string$classthe name of the class to load.
Returns:
string|false class name on success - false on failure
    {
        // optimistic (registered package) case.
        $package = substr($class, 0, strpos($class, '_'));
        if (isset(static::$_packages[$package])) {
            $file = static::$_packages[$package] . '/'
                  . str_replace('_', '/', substr($class, strlen($package) + 1))
                  . '.php';

            // if file exists (via direct or 'special' mapping), include it.
            if (is_readable($file)
                || $file = static::_resolveSpecialClass($class, $package)
            ) {
                include $file;
                return $class;
            }
        }

        // general case: autodiscover the path from the class name
        $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
        self::_securityCheck($file);
        if (Zend_Loader::isReadable($file)) {
            include $file;
            return $class;
        }

        return false;
    }
static P4Cms_Loader::getPackagePaths ( ) [static]

Get the set of registered module paths.

Returns:
array an array of registered module paths keyed on namespaces.
    {
        return self::$_packages;
    }
static P4Cms_Loader::setPackagePaths ( paths) [static]

Set module paths to the given array of paths keyed on namespaces.

Parameters:
array$pathsan array of module paths keyed on namespaces.
    {
        self::$_packages = $paths;
    }

Member Data Documentation

P4Cms_Loader::$_packages = array() [static, protected]

The documentation for this class was generated from the following file: