<?php
namespace Moto\System\Plugins; use Moto; use Zend\Loader\SplAutoloader; use Zend\Filter\Word\CamelCaseToUnderscore; class Autoloader implements SplAutoloader { protected static $_instance; protected $_classPrefix = 'MotoPlugin\\'; protected $_pluginsPath = '@plugins'; protected $_folderPathPrefix = array('_', '-',); public function __construct($options = null) { static::$_instance = $this; } public function setOptions($options) { return $this; } public function autoload($class) { if (0 !== strpos($class, $this->_classPrefix)) { return false; } Moto\Hook::trigger(Moto\Hook::PLUGIN_AUTO_LOADER__LOAD_CLASS, $class, $this); if (class_exists($class, false)) { return true; } $parts = explode('\\', $class, 4); if (count($parts) < 4) { return false; } $pluginAbsolutePath = null; foreach ($this->_folderPathPrefix as $pathPrefix) { $pluginAuthor = $parts[1]; $pluginName = $parts[2]; $path = $this->_pluginsPath . '/' . Moto\Util::toSnakeCase($pluginAuthor, $pathPrefix) . '/' . Moto\Util::toSnakeCase($pluginName, $pathPrefix); $absolutePath = Moto\System::getAbsolutePath($path); if (is_dir($absolutePath)) { $pluginAbsolutePath = $absolutePath; break; } } if ($pluginAbsolutePath === null) { return false; } $absoluteFilePath = $pluginAbsolutePath . '/library/' . $parts[3] . '.php'; $absoluteFilePath = Moto\Util::sanitizePath($absoluteFilePath); if (file_exists($absoluteFilePath)) { include_once $absoluteFilePath; } if (class_exists($class)) { return true; } return false; } public function register() { spl_autoload_register(array($this, 'autoload')); } public static function getInstance() { if (static::$_instance === null) { static::$_instance = new self(); } return static::$_instance; } } 