Browse Source

refactored Autoloader; added support for json encoded format for environment "hosts" option

1.0
Vladimir Grigor 11 years ago
parent
commit
5ce3394b3c
  1. 42
      Mage/Autoload.php
  2. 12
      Mage/Command/Factory.php
  3. 40
      Mage/Config.php
  4. 13
      Mage/Task/Factory.php
  5. 5
      bin/mage

42
Mage/Autoload.php

@ -20,34 +20,40 @@ class Autoload
/** /**
* Autoload a Class by it's Class Name * Autoload a Class by it's Class Name
* @param string $className * @param string $className
* @return boolean
*/ */
public static function autoload($className) public function autoLoad($className)
{ {
$className = ltrim($className, '/');
$postfix = '/' . str_replace(array('_', '\\'), '/', $className . '.php');
//Try to load a normal Mage class (or Task). Think that Mage component is compiled to .phar
$baseDir = dirname(dirname(__FILE__)); $baseDir = dirname(dirname(__FILE__));
$classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php'); $classFileWithinPhar = $baseDir . $postfix;
require_once $classFile; 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
* Checks if a Class can be loaded. $classFileOutsidePhar = getcwd() . '/.mage/tasks' . $postfix;
* @param string $className if($this->isReadable($classFileOutsidePhar)){
* @return boolean require_once $classFileOutsidePhar;
*/ return true;
public static function isLoadable($className) }
{
$baseDir = dirname(dirname(__FILE__)); return false;
$classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php');
return (file_exists($classFile) && is_readable($classFile));
} }
/** /**
* Loads a User's Tasks * Checks if a file can be read.
* @param string $taskName * @param string $filePath
* @return boolean
*/ */
public static function loadUserTask($taskName) public function isReadable($filePath)
{ {
$classFile = getcwd() . '/.mage/tasks/' . ucfirst($taskName) . '.php'; return is_readable($filePath);
require_once $classFile;
} }
} }

12
Mage/Command/Factory.php

@ -38,18 +38,14 @@ class Factory
$commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName))); $commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName)));
$className = 'Mage\\Command\\BuiltIn\\' . $commandName . 'Command'; $className = 'Mage\\Command\\BuiltIn\\' . $commandName . 'Command';
if (Autoload::isLoadable($className)) { /** @var AbstractCommand $instance */
$instance = new $className; $instance = new $className;
assert($instance instanceOf AbstractCommand); if(!is_a($instance, "Mage\Command\AbstractCommand")) {
$instance->setConfig($config);
} else {
throw new Exception('Command not found.');
}
if(!($instance instanceOf AbstractCommand)) {
throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.'); throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.');
} }
$instance->setConfig($config);
return $instance; return $instance;
} }
} }

40
Mage/Config.php

@ -24,6 +24,7 @@ use Exception;
*/ */
class Config class Config
{ {
const HOST_NAME_LENGTH = 1000;
/** /**
* Arguments loaded * Arguments loaded
* @var array * @var array
@ -353,13 +354,7 @@ class Config
if (is_array($envConfig['hosts'])) { if (is_array($envConfig['hosts'])) {
$hosts = (array) $envConfig['hosts']; $hosts = (array) $envConfig['hosts'];
} else if (is_string($envConfig['hosts']) && file_exists($envConfig['hosts']) && is_readable($envConfig['hosts'])) { } else if (is_string($envConfig['hosts']) && file_exists($envConfig['hosts']) && is_readable($envConfig['hosts'])) {
$fileContent = fopen($envConfig['hosts'], 'r'); $hosts = $this->getHostsFromFile($envConfig['hosts']);
while (($host = fgets($fileContent)) == true) {
$host = trim($host);
if ($host != '') {
$hosts[] = $host;
}
}
} }
} }
@ -565,7 +560,7 @@ class Config
* @param mixed $default * @param mixed $default
* @return mixed * @return mixed
*/ */
protected function getEnvironmentOption($option, $default = array()) public function getEnvironmentOption($option, $default = array())
{ {
$config = $this->getEnvironmentConfig(); $config = $this->getEnvironmentConfig();
if (isset($config[$option])) { if (isset($config[$option])) {
@ -608,4 +603,33 @@ class Config
return $this->environmentConfig; 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;
}
} }

13
Mage/Task/Factory.php

@ -48,22 +48,15 @@ class Factory
$taskName = str_replace(' ', '', $taskName); $taskName = str_replace(' ', '', $taskName);
if (strpos($taskName, '/') === false) { if (strpos($taskName, '/') === false) {
Autoload::loadUserTask($taskName); $className = $taskName;
$className = 'Task\\' . ucfirst($taskName);
} else { } else {
$taskName = str_replace(' ', '\\', ucwords(str_replace('/', ' ', $taskName))); $className = 'Mage\\Task\\BuiltIn\\' . str_replace(' ', '\\', ucwords(str_replace('/', ' ', $taskName))) . 'Task';
$className = 'Mage\\Task\\BuiltIn\\' . $taskName . 'Task';
} }
if (class_exists($className) || Autoload::isLoadable($className)) {
$instance = new $className($taskConfig, $inRollback, $stage, $taskParameters); $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.'); throw new Exception('The Task ' . $taskName . ' must be an instance of Mage\Task\AbstractTask.');
} }

5
bin/mage

@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Mage\Autoload;
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
$baseDir = dirname(dirname(__FILE__)); $baseDir = dirname(dirname(__FILE__));
@ -18,7 +20,8 @@ define('MAGALLANES_DIRECTORY', $baseDir);
// Preload // Preload
require_once $baseDir . '/Mage/Autoload.php'; require_once $baseDir . '/Mage/Autoload.php';
spl_autoload_register(array('Mage\\Autoload', 'autoload')); $loader = new Autoload();
spl_autoload_register(array($loader, 'autoLoad'));
// Clean arguments // Clean arguments
array_shift($argv); array_shift($argv);

Loading…
Cancel
Save