mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	refactored Autoloader; added support for json encoded format for environment "hosts" option
This commit is contained in:
		
							parent
							
								
									6ecaf41336
								
							
						
					
					
						commit
						5ce3394b3c
					
				@ -20,34 +20,40 @@ class Autoload
 | 
			
		||||
	/**
 | 
			
		||||
	 * Autoload a Class by it's Class Name
 | 
			
		||||
	 * @param string $className
 | 
			
		||||
	 */
 | 
			
		||||
    public static function autoload($className)
 | 
			
		||||
    {
 | 
			
		||||
        $baseDir = dirname(dirname(__FILE__));
 | 
			
		||||
        $classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php');
 | 
			
		||||
        require_once $classFile;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if a Class can be loaded.
 | 
			
		||||
     * @param string $className
 | 
			
		||||
     * @return boolean
 | 
			
		||||
	 */
 | 
			
		||||
    public static function isLoadable($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__));
 | 
			
		||||
        $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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -38,18 +38,14 @@ class Factory
 | 
			
		||||
 | 
			
		||||
        $commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName)));
 | 
			
		||||
        $className = 'Mage\\Command\\BuiltIn\\' . $commandName . 'Command';
 | 
			
		||||
        if (Autoload::isLoadable($className)) {
 | 
			
		||||
        /** @var AbstractCommand $instance */
 | 
			
		||||
        $instance = new $className;
 | 
			
		||||
            assert($instance instanceOf AbstractCommand);
 | 
			
		||||
            $instance->setConfig($config);
 | 
			
		||||
        } else {
 | 
			
		||||
            throw new Exception('Command not found.');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if(!($instance instanceOf AbstractCommand)) {
 | 
			
		||||
        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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -24,6 +24,7 @@ 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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';
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        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.');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										5
									
								
								bin/mage
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								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);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user