Skip to content

Commit

Permalink
Implement #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Bui Sy Nguyen committed Apr 28, 2016
1 parent 2e66505 commit f0d338a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Loader
* @subpackage Autoloader
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/** Zend_Loader */
require_once 'Zend/Loader.php';
///////////////////////////////////////////////////////////////////////////////
//
// © Copyright f-project.net 2010-present.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

namespace fproject\amf\loader;
use fproject\amf\AmfException;

/**
* Autoloader stack and namespace autoloader
* Class and namespace autoloader
*
* @uses Zend_Loader_Autoloader
* @package Zend_Loader
* @subpackage Autoloader
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Loader_Autoloader
class Autoloader
{
/**
* @var Zend_Loader_Autoloader Singleton instance
* @var Autoloader $_instance Singleton instance
*/
protected static $_instance;

Expand All @@ -47,7 +39,7 @@ class Zend_Loader_Autoloader
/**
* @var array Default autoloader callback
*/
protected $_defaultAutoloader = array('Zend_Loader', 'loadClass');
protected $_defaultAutoloader = array('fproject\amf\loader\Loader', 'loadClass');

/**
* @var bool Whether or not to act as a fallback autoloader
Expand Down Expand Up @@ -85,7 +77,7 @@ class Zend_Loader_Autoloader
/**
* Retrieve singleton instance
*
* @return Zend_Loader_Autoloader
* @return Autoloader
*/
public static function getInstance()
{
Expand Down Expand Up @@ -116,7 +108,7 @@ public static function autoload($class)
$self = self::getInstance();

foreach ($self->getClassAutoloaders($class) as $autoloader) {
if ($autoloader instanceof Zend_Loader_Autoloader_Interface) {
if ($autoloader instanceof AutoloaderInterface) {
if ($autoloader->autoload($class)) {
return true;
}
Expand All @@ -139,12 +131,12 @@ public static function autoload($class)
*
* @param string|array $callback PHP callback
* @return $this
* @throws \fproject\amf\AmfException
* @throws AmfException
*/
public function setDefaultAutoloader($callback)
{
if (!is_callable($callback)) {
throw new \fproject\amf\AmfException('Invalid callback specified for default autoloader');
throw new AmfException('Invalid callback specified for default autoloader');
}

$this->_defaultAutoloader = $callback;
Expand All @@ -164,8 +156,8 @@ public function getDefaultAutoloader()
/**
* Set several autoloader callbacks at once
*
* @param array $autoloaders Array of PHP callbacks (or Zend_Loader_Autoloader_Interface implementations) to act as autoloaders
* @return Zend_Loader_Autoloader
* @param array $autoloaders Array of PHP callbacks (or AutoloaderInterface implementations) to act as autoloaders
* @return Autoloader
*/
public function setAutoloaders(array $autoloaders)
{
Expand Down Expand Up @@ -202,15 +194,15 @@ public function getNamespaceAutoloaders($namespace)
* Register a namespace to autoload
*
* @param string|array $namespace
* @return Zend_Loader_Autoloader
* @throws \fproject\amf\AmfException
* @return Autoloader
* @throws AmfException
*/
public function registerNamespace($namespace)
{
if (is_string($namespace)) {
$namespace = (array) $namespace;
} elseif (!is_array($namespace)) {
throw new \fproject\amf\AmfException('Invalid namespace provided');
throw new AmfException('Invalid namespace provided');
}

foreach ($namespace as $ns) {
Expand All @@ -225,15 +217,15 @@ public function registerNamespace($namespace)
* Unload a registered autoload namespace
*
* @param string|array $namespace
* @return Zend_Loader_Autoloader
* @throws \fproject\amf\AmfException
* @return Autoloader
* @throws AmfException
*/
public function unregisterNamespace($namespace)
{
if (is_string($namespace)) {
$namespace = (array) $namespace;
} elseif (!is_array($namespace)) {
throw new \fproject\amf\AmfException('Invalid namespace provided');
throw new AmfException('Invalid namespace provided');
}

foreach ($namespace as $ns) {
Expand All @@ -259,7 +251,7 @@ public function setZfPath($spec, $version = 'latest')
$path = $spec;
if (is_array($spec)) {
if (!isset($spec['path'])) {
throw new \fproject\amf\AmfException('No path specified for ZF');
throw new AmfException('No path specified for ZF');
}
$path = $spec['path'];
if (isset($spec['version'])) {
Expand All @@ -284,7 +276,7 @@ public function getZfPath()
* Get or set the value of the "suppress not found warnings" flag
*
* @param null|bool $flag
* @return bool|Zend_Loader_Autoloader Returns boolean if no argument is passed, object instance otherwise
* @return bool|Autoloader Returns boolean if no argument is passed, object instance otherwise
*/
public function suppressNotFoundWarnings($flag = null)
{
Expand All @@ -299,7 +291,7 @@ public function suppressNotFoundWarnings($flag = null)
* Indicate whether or not this autoloader should be a fallback autoloader
*
* @param bool $flag
* @return Zend_Loader_Autoloader
* @return Autoloader
*/
public function setFallbackAutoloader($flag)
{
Expand Down Expand Up @@ -374,9 +366,9 @@ public function getClassAutoloaders($class)
/**
* Add an autoloader to the beginning of the stack
*
* @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
* @param object|array|string $callback PHP callback or AutoloaderInterface implementation
* @param string|array $namespace Specific namespace(s) under which to register callback
* @return Zend_Loader_Autoloader
* @return Autoloader
*/
public function unshiftAutoloader($callback, $namespace = '')
{
Expand All @@ -397,9 +389,9 @@ public function unshiftAutoloader($callback, $namespace = '')
/**
* Append an autoloader to the autoloader stack
*
* @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
* @param object|array|string $callback PHP callback or AutoloaderInterface implementation
* @param string|array $namespace Specific namespace(s) under which to register callback
* @return Zend_Loader_Autoloader
* @return Autoloader
*/
public function pushAutoloader($callback, $namespace = '')
{
Expand All @@ -420,9 +412,9 @@ public function pushAutoloader($callback, $namespace = '')
/**
* Remove an autoloader from the autoloader stack
*
* @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
* @param object|array|string $callback PHP callback or AutoloaderInterface implementation
* @param null|string|array $namespace Specific namespace(s) from which to remove autoloader
* @return Zend_Loader_Autoloader
* @return Autoloader
*/
public function removeAutoloader($callback, $namespace = null)
{
Expand Down Expand Up @@ -481,7 +473,7 @@ protected function _autoload($class)
call_user_func($callback, $class);
}
return $class;
} catch (Zend_Exception $e) {
} catch (AmfException $e) {
return false;
}
}
Expand All @@ -491,7 +483,7 @@ protected function _autoload($class)
*
* @param array $autoloaders
* @param string $namespace
* @return Zend_Loader_Autoloader
* @return Autoloader
*/
protected function _setNamespaceAutoloaders(array $autoloaders, $namespace = '')
{
Expand All @@ -506,7 +498,7 @@ protected function _setNamespaceAutoloaders(array $autoloaders, $namespace = '')
* @param string $path
* @param string $version
* @return mixed
* @throws \fproject\amf\AmfException
* @throws AmfException
*/
protected function _getVersionPath($path, $version)
{
Expand All @@ -518,7 +510,7 @@ protected function _getVersionPath($path, $version)

$availableVersions = $this->_getAvailableVersions($path, $version);
if (empty($availableVersions)) {
throw new \fproject\amf\AmfException('No valid ZF installations discovered');
throw new AmfException('No valid ZF installations discovered');
}

$matchedVersion = array_pop($availableVersions);
Expand All @@ -530,7 +522,7 @@ protected function _getVersionPath($path, $version)
*
* @param string $version
* @return string "latest", "major", "minor", or "specific"
* @throws \fproject\amf\AmfException if version string contains too many dots
* @throws AmfException if version string contains too many dots
*/
protected function _getVersionType($version)
{
Expand All @@ -547,7 +539,7 @@ protected function _getVersionType($version)
return 'minor';
}
if (3 < $count) {
throw new \fproject\amf\AmfException('Invalid version string provided');
throw new AmfException('Invalid version string provided');
}
return 'specific';
}
Expand All @@ -558,12 +550,12 @@ protected function _getVersionType($version)
* @param string $path
* @param string $version
* @return array
* @throws \fproject\amf\AmfException
* @throws AmfException
*/
protected function _getAvailableVersions($path, $version)
{
if (!is_dir($path)) {
throw new \fproject\amf\AmfException('Invalid ZF path provided');
throw new AmfException('Invalid ZF path provided');
}

$path = rtrim($path, '/');
Expand Down
38 changes: 38 additions & 0 deletions fproject/amf/loader/AutoloaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
///////////////////////////////////////////////////////////////////////////////
//
// © Copyright f-project.net 2010-present.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

namespace fproject\amf\loader;

/**
* Autoloader interface
*
*/
interface AutoloaderInterface
{
/**
* Autoload a class
*
* @abstract
* @param string $class
* @return mixed
* False [if unable to load $class]
* get_class($class) [if $class is successfully loaded]
*/
public function autoload($class);
}

0 comments on commit f0d338a

Please sign in to comment.