Encapsulate PSR-0 in \Mibew\Autoloader class

This commit is contained in:
Dmitriy Simushev 2014-02-26 12:08:16 +00:00
parent bb4e935303
commit 409c455bd1
3 changed files with 84 additions and 41 deletions

View File

@ -0,0 +1,82 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* 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 Mibew;
/**
* Autoloader for classes which implements PSR-0 standard.
*/
class Autoloader
{
/**
* Base path for autoloaded classes.
*
* @var string
*/
private $basePath;
/**
* Class loader.
*
* Map class name to file system using PSR-0 and base path specified via
* \Mibew\Autoloader::register().
*
* @param string $class_name Class name to load.
*/
public function autoload($class_name)
{
$class_name = ltrim($class_name, '\\');
$file_name = '';
$namespace = '';
if ($last_ns_pos = strrpos($class_name, '\\')) {
$namespace = substr($class_name, 0, $last_ns_pos);
$class_name = substr($class_name, $last_ns_pos + 1);
$file_name = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$file_name = $this->basePath . DIRECTORY_SEPARATOR . $file_name
. str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
if (is_file($file_name)) {
include_once($file_name);
}
}
/**
* Register a new instance as an SPL autoloader.
*
* @param string $base_path Base path for classes-to-files mapping.
*
* @return \Mibew\Autoloader Registered autoloader instance.
*/
public static function register($base_path)
{
$loader = new self($base_path);
spl_autoload_register(array($loader, 'autoload'));
return $loader;
}
/**
* Class constructor.
*
* @param string $base_path Base path for classes-to-files mapping.
*/
protected function __construct($base_path)
{
$this->basePath = rtrim($base_path, '/\\');
}
}

View File

@ -1,39 +0,0 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* 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.
*/
/**
* Autoloader for classes which implements PSR-0 standard.
*
* @param string $class_name Fully qualified name of the class
*/
function class_autoload($class_name)
{
$base_dir = MIBEW_FS_ROOT . DIRECTORY_SEPARATOR . 'libs' .
DIRECTORY_SEPARATOR . 'classes';
$class_name = ltrim($class_name, '\\');
$file_name = '';
$namespace = '';
if ($last_ns_pos = strrpos($class_name, '\\')) {
$namespace = substr($class_name, 0, $last_ns_pos);
$class_name = substr($class_name, $last_ns_pos + 1);
$file_name = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$file_name .= str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
require $base_dir . DIRECTORY_SEPARATOR . $file_name;
}

View File

@ -44,8 +44,8 @@ define('MIBEW_WEB_ROOT', $mibewroot);
require_once(MIBEW_FS_ROOT . '/libs/common/constants.php'); require_once(MIBEW_FS_ROOT . '/libs/common/constants.php');
// Initialize classes autoloading // Initialize classes autoloading
require_once(MIBEW_FS_ROOT . '/libs/common/autoload.php'); require_once(MIBEW_FS_ROOT . '/libs/classes/Mibew/Autoloader.php');
spl_autoload_register('class_autoload'); Mibew\Autoloader::register(MIBEW_FS_ROOT . '/libs/classes');
// Initialize external dependencies // Initialize external dependencies
require_once(MIBEW_FS_ROOT . '/vendor/autoload.php'); require_once(MIBEW_FS_ROOT . '/vendor/autoload.php');