Permalink
Browse files

Simplify autoloader, based on PSR-4

  • Loading branch information...
1 parent 6f2b45b commit cb30821815f829a2a9013f286358d49d114d98c8 @haraldpdl committed Feb 24, 2014
Showing with 24 additions and 190 deletions.
  1. +3 −5 index.php
  2. +19 −39 osCommerce/OM/Core/Autoloader.php
  3. +2 −4 osCommerce/OM/Core/OSCOM.php
  4. +0 −142 osCommerce/OM/External/SplClassLoader.php
View
@@ -1,12 +1,11 @@
<?php
/**
* osCommerce Online Merchant
- *
- * @copyright Copyright (c) 2012 osCommerce; http://www.oscommerce.com
+ *
+ * @copyright Copyright (c) 2014 osCommerce; http://www.oscommerce.com
* @license BSD License; http://www.oscommerce.com/bsdlicense.txt
*/
- use osCommerce\OM\Core\Autoloader;
use osCommerce\OM\Core\OSCOM;
define('OSCOM_TIMESTAMP_START', microtime());
@@ -16,8 +15,7 @@
define('OSCOM_PUBLIC_BASE_DIRECTORY', __DIR__ . '/');
require('osCommerce/OM/Core/Autoloader.php');
- $OSCOM_Autoloader = new Autoloader('osCommerce\OM');
- $OSCOM_Autoloader->register();
+ spl_autoload_register('osCommerce\\OM\\Core\\Autoloader::load');
OSCOM::initialize();
@@ -1,54 +1,34 @@
<?php
/**
* osCommerce Online Merchant
- *
- * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
+ *
+ * @copyright Copyright (c) 2014 osCommerce; http://www.oscommerce.com
* @license BSD License; http://www.oscommerce.com/bsdlicense.txt
*/
- namespace {
- if ( !class_exists('SplClassLoader') ) {
- include(__DIR__ . '/../External/SplClassLoader.php');
- }
- }
-
- namespace osCommerce\OM\Core {
- class Autoloader extends \SplClassLoader {
- public function loadClass($className) {
- if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
- $fileName = '';
- $namespace = '';
+ namespace osCommerce\OM\Core;
- if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
- $namespace = substr($className, 0, $lastNsPos);
- $className = substr($className, $lastNsPos + 1);
- $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
- }
+ define('OSCOM_BASE_DIRECTORY', realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR);
- $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
+ class Autoloader {
+ public static function load($class) {
+ $prefix = 'osCommerce\\OM\\';
- $includeFile = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
-
-// HPDL; require() returns a "file does not exist" error when \class_exists() is
-// used. Instead, use file_exists() and include()
-
-// HPDL: Check for and include custom version
- if ( strpos($includeFile, 'osCommerce' . DIRECTORY_SEPARATOR . 'OM' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR) !== false ) {
- $includeFile = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $includeFile;
+// only auto load related classes
+ $len = strlen($prefix);
+ if ( strncmp($prefix, $class, $len) !== 0 ) {
+ return false;
+ }
- $custom_includeFile = str_replace('osCommerce' . DIRECTORY_SEPARATOR . 'OM' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR, 'osCommerce' . DIRECTORY_SEPARATOR . 'OM' . DIRECTORY_SEPARATOR . 'Custom' . DIRECTORY_SEPARATOR, $includeFile);
+ $class = substr($class, $len);
- if (file_exists($custom_includeFile)) {
- include ($custom_includeFile);
- return true;
- }
- }
+ $file = OSCOM_BASE_DIRECTORY . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
+ $custom = str_replace('osCommerce' . DIRECTORY_SEPARATOR . 'OM' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR, 'osCommerce' . DIRECTORY_SEPARATOR . 'OM' . DIRECTORY_SEPARATOR . 'Custom' . DIRECTORY_SEPARATOR, $file);
- if (file_exists($includeFile)) {
- include ($includeFile);
- return true;
- }
- }
+ if ( file_exists($custom) ) {
+ require($custom);
+ } else if ( file_exists($file) ) {
+ require($file);
}
}
}
@@ -1,15 +1,13 @@
<?php
/**
* osCommerce Online Merchant
- *
- * @copyright Copyright (c) 2012 osCommerce; http://www.oscommerce.com
+ *
+ * @copyright Copyright (c) 2014 osCommerce; http://www.oscommerce.com
* @license BSD License; http://www.oscommerce.com/bsdlicense.txt
*/
namespace osCommerce\OM\Core;
- define('OSCOM_BASE_DIRECTORY', realpath(__DIR__ . '/../') . '/');
-
class OSCOM {
const TIMESTAMP_START = OSCOM_TIMESTAMP_START;
const BASE_DIRECTORY = OSCOM_BASE_DIRECTORY;
@@ -1,142 +0,0 @@
-<?php
-/**
- * SplClassLoader implementation that implements the technical interoperability
- * standards for PHP 5.3 namespaces and class names.
- *
- * http://groups.google.com/group/php-standards/web/final-proposal
- *
- * // Example which loads classes for the Doctrine Common package in the
- * // Doctrine\Common namespace.
- * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
- * $classLoader->register();
- *
- * @author Jonathan H. Wage <jonwage@gmail.com>
- * @author Roman S. Borschel <roman@code-factory.org>
- * @author Matthew Weier O'Phinney <matthew@zend.com>
- * @author Kris Wallsmith <kris.wallsmith@gmail.com>
- * @author Fabien Potencier <fabien.potencier@symfony-project.org>
- * @author Harald Ponce de leon <hpdl@oscommerce.com>
- */
-class SplClassLoader
-{
- protected $_fileExtension = '.php';
- protected $_namespace;
- protected $_includePath;
- protected $_namespaceSeparator = '\\';
-
- /**
- * Creates a new <tt>SplClassLoader</tt> that loads classes of the
- * specified namespace.
- *
- * @param string $ns The namespace to use.
- */
- public function __construct($ns = null, $includePath = null)
- {
- $this->_namespace = $ns;
- $this->_includePath = $includePath;
- }
-
- /**
- * Sets the namespace separator used by classes in the namespace of this class loader.
- *
- * @param string $sep The separator to use.
- */
- public function setNamespaceSeparator($sep)
- {
- $this->_namespaceSeparator = $sep;
- }
-
- /**
- * Gets the namespace seperator used by classes in the namespace of this class loader.
- *
- * @return void
- */
- public function getNamespaceSeparator()
- {
- return $this->_namespaceSeparator;
- }
-
- /**
- * Sets the base include path for all class files in the namespace of this class loader.
- *
- * @param string $includePath
- */
- public function setIncludePath($includePath)
- {
- $this->_includePath = $includePath;
- }
-
- /**
- * Gets the base include path for all class files in the namespace of this class loader.
- *
- * @return string $includePath
- */
- public function getIncludePath()
- {
- return $this->_includePath;
- }
-
- /**
- * Sets the file extension of class files in the namespace of this class loader.
- *
- * @param string $fileExtension
- */
- public function setFileExtension($fileExtension)
- {
- $this->_fileExtension = $fileExtension;
- }
-
- /**
- * Gets the file extension of class files in the namespace of this class loader.
- *
- * @return string $fileExtension
- */
- public function getFileExtension()
- {
- return $this->_fileExtension;
- }
-
- /**
- * Installs this class loader on the SPL autoload stack.
- */
- public function register()
- {
- spl_autoload_register(array($this, 'loadClass'));
- }
-
- /**
- * Uninstalls this class loader from the SPL autoloader stack.
- */
- public function unregister()
- {
- spl_autoload_unregister(array($this, 'loadClass'));
- }
-
- /**
- * Loads the given class or interface.
- *
- * @param string $className The name of the class to load.
- * @return void
- */
- public function loadClass($className)
- {
- if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
- $fileName = '';
- $namespace = '';
- if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
- $namespace = substr($className, 0, $lastNsPos);
- $className = substr($className, $lastNsPos + 1);
- $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
- }
- $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
-
-// HPDL; Only include the file if it exists otherwise \class_exists() returns a
-// "file does not exist" error.
- $includeFile = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
-
- if (file_exists($includeFile)) {
- include ($includeFile);
- }
- }
- }
-}

0 comments on commit cb30821

Please sign in to comment.