Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * @package exTSend
- * @version SVN: $Id: NamespacePath.php 8 2009-12-14 20:55:14Z gabriel.schuster $
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- * @copyright Copyright 2009, actra.development, Korntal-Muenchingen
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * NEW BSD LICENSE
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- *
- * exTSend.com - Copyright (c) 2009, actra.development
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and / or other materials provided with the
- * distribution.
- * - Neither the name of actra.development nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
- /**
- * An autoloader that supports specific paths for given class prefixes (namespaces)
- * The longest match wins:
- * Given autoloader configuration:
- * MyApp_ => /path/to/myapp
- * MyApp_Abc_ => /path/to/other/abc
- * Requested class:
- * MyApp_Abc_Test
- * Loaded file:
- * /path/to/other/abc/Test.php
- *
- * @package exTSend_Loader_Autoloader_NamespacePath
- * @author Gabriel Schuster <sourcecode@extsend.com>
- * @copyright Copyright 2009, actra.development, Korntal-Muenchingen
- */
- class exTSend_Loader_Autoloader_NamespacePath implements Zend_Loader_Autoloader_Interface {
- /**
- * @name $_instance
- * @staticvar exTSend_Loader_Autoloader_NamespacePath
- * @access protected
- */
- protected static $_instance = NULL;
- /**
- * @name $_namespaces
- * @var array
- * @access protected
- */
- protected $_namespaces = array();
- /**
- * Constructor
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- *
- * @access public
- * @return void
- */
- protected function __construct() {
- }
- /**
- * Singleton instantiator
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- *
- * @access
- * @return unknown_type
- */
- public static function getInstance() {
- if(is_null(self::$_instance) || TRUE !== (self::$_instance instanceof exTSend_Loader_Autoloader_NamespacePath)) {
- self::$_instance = new exTSend_Loader_Autoloader_NamespacePath();
- }
- return self::$_instance;
- }
- /**
- * Add a namespace (does not overwrite previously set namespace path)
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- *
- * @access public
- * @param string $namespace
- * @param string $path
- * @return exTSend_Loader_Autoloader_NamespacePath
- */
- public function addNamespace($namespace, $path) {
- $namespace = trim($namespace, '_');
- $segments = preg_split('/_/', $namespace, NULL, PREG_SPLIT_NO_EMPTY);
- if(count($segments)) {
- $reference =& $this->_namespaces;
- foreach($segments as $index => $segment) {
- if(!array_key_exists($segment, $reference)) {
- $reference[$segment] = array('path' => NULL, 'sub' => array());
- }
- if($index === count($segments) - 1 && is_null($reference[$segment]['path'])) {
- Zend_Loader_Autoloader::getInstance()->unshiftAutoloader($this, implode('_', $segments).'_');
- $reference[$segment]['path'] = preg_replace(
- '#[/\\\]+#',
- DIRECTORY_SEPARATOR,
- preg_replace(
- '#^[/\\\]+#',
- DIRECTORY_SEPARATOR,
- preg_replace(
- '#[/\\\]+$#',
- '',
- (string)$path
- )
- )
- ).DIRECTORY_SEPARATOR;
- }
- $reference =& $reference[$segment]['sub'];
- }
- }
- return $this;
- }
- /**
- * Set a namespace (overwrites previously set namespace path)
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- *
- * @access public
- * @param string $namespace
- * @param string $path
- * @return exTSend_Loader_Autoloader_NamespacePath
- */
- public function setNamespace($namespace, $path) {
- $namespace = trim($namespace, '_');
- $segments = preg_split('/_/', $namespace, NULL, PREG_SPLIT_NO_EMPTY);
- if(count($segments)) {
- $reference =& $this->_namespaces;
- foreach($segments as $index => $segment) {
- if(!array_key_exists($segment, $reference)) {
- $reference[$segment] = array('path' => NULL, 'sub' => array());
- }
- if($index === count($segments) - 1) {
- if(is_null($reference[$segment]['path'])) {
- Zend_Loader_Autoloader::getInstance()->unshiftAutoloader($this, implode('_', $segments).'_');
- }
- $reference[$segment]['path'] = preg_replace(
- '#[/\\\]+#',
- DIRECTORY_SEPARATOR,
- preg_replace(
- '#^[/\\\]+#',
- DIRECTORY_SEPARATOR,
- preg_replace(
- '#[/\\\]+$#',
- '',
- (string)$path
- )
- )
- ).DIRECTORY_SEPARATOR;
- }
- $reference =& $reference[$segment]['sub'];
- }
- }
- return $this;
- }
- /**
- * Remove a namespace
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- *
- * @access public
- * @param string $namespace
- * @param boolean $withChildren
- * @return exTSend_Loader_Autoloader_NamespacePath
- */
- public function removeNamespace($namespace, $withChildren = FALSE) {
- $namespace = trim($namespace, '_');
- $segments = preg_split('/_/', $namespace, NULL, PREG_SPLIT_NO_EMPTY);
- if(count($segments)) {
- $reference =& $this->_namespaces;
- foreach($segments as $index => $segment) {
- if(!array_key_exists($segment, $reference)) {
- break;
- }
- if($index === count($segments) - 1) {
- if(TRUE === (bool)$withChildren) {
- unset($reference[$segment]);
- }
- else {
- $reference[$segment]['path'] = NULL;
- }
- }
- if(!array_key_exists($segment, $reference)) {
- break;
- }
- $reference =& $reference[$segment]['sub'];
- }
- }
- return $this;
- }
- /**
- * Autoload a class
- *
- * @author Gabriel Schuster <sourcecode@extsend.com>
- *
- * @access
- * @param string $class
- * @return NULL|boolean|string
- */
- public function autoload($class) {
- if(class_exists($class, FALSE) || interface_exists($class, FALSE)) {
- return;
- }
- $class = trim($class, '_');
- $segments = preg_split('/_/', $class, NULL, PREG_SPLIT_NO_EMPTY);
- if(count($segments) && array_key_exists($segments[0], $this->_namespaces)) {
- $currentNamespace = $segments[0].'_';
- $lastNamespace = !is_null($this->_namespaces[$segments[0]]['path']) ? $segments[0].'_' : NULL;
- $lastPath = !is_null($this->_namespaces[$segments[0]]['path']) ? $this->_namespaces[$segments[0]]['path'] : NULL;
- $reference =& $this->_namespaces[$segments[0]]['sub'];
- foreach(array_slice($segments, 1) as $index => $segment) {
- if($index === count($segments) - 2) {
- break;
- }
- $currentNamespace .= $segment.'_';
- if(array_key_exists($segment, $reference)) {
- if(!is_null($reference[$segment]['path'])) {
- $lastNamespace = $currentNamespace;
- $lastPath = $reference[$segment]['path'];
- }
- }
- else {
- break;
- }
- $reference =& $reference[$segment]['sub'];
- }
- if(is_null($lastNamespace) || is_null($lastPath)) {
- // No matching namespace with a configured path found
- return FALSE;
- }
- $filename = str_replace('_', DIRECTORY_SEPARATOR, substr(implode('_', $segments), strlen($lastNamespace))).'.php';
- if(preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
- throw new exTSend_Loader_Autoloader_NamespacePath_Exception(
- 'Illegal character in filename detected.',
- exTSend_Loader_Autoloader_NamespacePath_Exception::EXCEPTION_ILLEGAL_CHARACTER_IN_FILENAME
- );
- }
- if(!file_exists($lastPath.$filename)) {
- return FALSE;
- }
- return include_once $lastPath.$filename;
- }
- return FALSE;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement