diff --git a/Mage/Autoload.php b/Mage/Autoload.php index a9bcc2f..132c6c3 100644 --- a/Mage/Autoload.php +++ b/Mage/Autoload.php @@ -20,34 +20,40 @@ class Autoload /** * Autoload a Class by it's Class Name * @param string $className + * @return boolean */ - public static function autoload($className) + public function autoLoad($className) { - $baseDir = dirname(dirname(__FILE__)); - $classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - require_once $classFile; - } + $className = ltrim($className, '/'); + $postfix = '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - /** - * Checks if a Class can be loaded. - * @param string $className - * @return boolean - */ - public static function isLoadable($className) - { + //Try to load a normal Mage class (or Task). Think that Mage component is compiled to .phar $baseDir = dirname(dirname(__FILE__)); - $classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - return (file_exists($classFile) && is_readable($classFile)); + $classFileWithinPhar = $baseDir . $postfix; + if($this->isReadable($classFileWithinPhar)) + { + require_once $classFileWithinPhar; + return true; + } + + //Try to load a custom Task or Class. Notice that the path is absolute to CWD + $classFileOutsidePhar = getcwd() . '/.mage/tasks' . $postfix; + if($this->isReadable($classFileOutsidePhar)){ + require_once $classFileOutsidePhar; + return true; + } + + return false; } /** - * Loads a User's Tasks - * @param string $taskName + * Checks if a file can be read. + * @param string $filePath + * @return boolean */ - public static function loadUserTask($taskName) + public function isReadable($filePath) { - $classFile = getcwd() . '/.mage/tasks/' . ucfirst($taskName) . '.php'; - require_once $classFile; + return is_readable($filePath); } } diff --git a/Mage/Command/Factory.php b/Mage/Command/Factory.php index da87189..19c9452 100644 --- a/Mage/Command/Factory.php +++ b/Mage/Command/Factory.php @@ -38,18 +38,14 @@ class Factory $commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName))); $className = 'Mage\\Command\\BuiltIn\\' . $commandName . 'Command'; - if (Autoload::isLoadable($className)) { - $instance = new $className; - assert($instance instanceOf AbstractCommand); - $instance->setConfig($config); - } else { - throw new Exception('Command not found.'); - } - - if(!($instance instanceOf AbstractCommand)) { + /** @var AbstractCommand $instance */ + $instance = new $className; + if(!is_a($instance, "Mage\Command\AbstractCommand")) { throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.'); } + $instance->setConfig($config); + return $instance; } } \ No newline at end of file diff --git a/Mage/Config.php b/Mage/Config.php index e7474d1..8b4b532 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -24,7 +24,8 @@ use Exception; */ class Config { - /** + const HOST_NAME_LENGTH = 1000; + /** * Arguments loaded * @var array */ @@ -353,13 +354,7 @@ class Config if (is_array($envConfig['hosts'])) { $hosts = (array) $envConfig['hosts']; } else if (is_string($envConfig['hosts']) && file_exists($envConfig['hosts']) && is_readable($envConfig['hosts'])) { - $fileContent = fopen($envConfig['hosts'], 'r'); - while (($host = fgets($fileContent)) == true) { - $host = trim($host); - if ($host != '') { - $hosts[] = $host; - } - } + $hosts = $this->getHostsFromFile($envConfig['hosts']); } } @@ -565,7 +560,7 @@ class Config * @param mixed $default * @return mixed */ - protected function getEnvironmentOption($option, $default = array()) + public function getEnvironmentOption($option, $default = array()) { $config = $this->getEnvironmentConfig(); if (isset($config[$option])) { @@ -608,4 +603,33 @@ class Config return $this->environmentConfig; } + /** + * @param string $filePath + * + * @return array + */ + protected function getHostsFromFile($filePath) + { + $handle = fopen($filePath, 'r'); + + $hosts = array(); + + try { + $fileContent = stream_get_contents($handle); + $hosts = json_decode($fileContent); + } catch (Exception $e) { + + rewind($handle); + //do it old-style: one host per line + while (($host = stream_get_line($handle, self::HOST_NAME_LENGTH)) !== false) { + $host = trim($host); + if (!empty($host)) { + $hosts[] = $host; + } + } + } + + return $hosts; + } + } diff --git a/Mage/Task/Factory.php b/Mage/Task/Factory.php index a8a7066..ecbc2b2 100644 --- a/Mage/Task/Factory.php +++ b/Mage/Task/Factory.php @@ -48,22 +48,15 @@ class Factory $taskName = str_replace(' ', '', $taskName); if (strpos($taskName, '/') === false) { - Autoload::loadUserTask($taskName); - $className = 'Task\\' . ucfirst($taskName); + $className = $taskName; } else { - $taskName = str_replace(' ', '\\', ucwords(str_replace('/', ' ', $taskName))); - $className = 'Mage\\Task\\BuiltIn\\' . $taskName . 'Task'; + $className = 'Mage\\Task\\BuiltIn\\' . str_replace(' ', '\\', ucwords(str_replace('/', ' ', $taskName))) . 'Task'; } + $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters); - if (class_exists($className) || Autoload::isLoadable($className)) { - $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters); - } else { - throw new ErrorWithMessageException('The Task "' . $taskName . '" doesn\'t exists.'); - } - - if (!($instance instanceOf AbstractTask)) { + if (!is_a($instance,'Mage\Task\AbstractTask')) { throw new Exception('The Task ' . $taskName . ' must be an instance of Mage\Task\AbstractTask.'); } diff --git a/bin/mage b/bin/mage index a9f0e22..9f6e436 100755 --- a/bin/mage +++ b/bin/mage @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Mage\Autoload; + date_default_timezone_set('UTC'); $baseDir = dirname(dirname(__FILE__)); @@ -18,7 +20,8 @@ define('MAGALLANES_DIRECTORY', $baseDir); // Preload require_once $baseDir . '/Mage/Autoload.php'; -spl_autoload_register(array('Mage\\Autoload', 'autoload')); +$loader = new Autoload(); +spl_autoload_register(array($loader, 'autoLoad')); // Clean arguments array_shift($argv);