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
 | 
						 * 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
 | 
				
			||||||
 | 
					        $classFileOutsidePhar = getcwd() . '/.mage/tasks' . $postfix;
 | 
				
			||||||
 | 
					        if($this->isReadable($classFileOutsidePhar)){
 | 
				
			||||||
 | 
					            require_once $classFileOutsidePhar;
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Checks if a Class can be loaded.
 | 
					     * Checks if a file can be read.
 | 
				
			||||||
     * @param string $className
 | 
					     * @param string $filePath
 | 
				
			||||||
     * @return boolean
 | 
					     * @return boolean
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static function isLoadable($className)
 | 
					    public function isReadable($filePath)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $baseDir = dirname(dirname(__FILE__));
 | 
					        return is_readable($filePath);
 | 
				
			||||||
        $classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php');
 | 
					 | 
				
			||||||
        return (file_exists($classFile) && is_readable($classFile));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Loads a User's Tasks
 | 
					 | 
				
			||||||
     * @param string $taskName
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public static function loadUserTask($taskName)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $classFile = getcwd() . '/.mage/tasks/' . ucfirst($taskName) . '.php';
 | 
					 | 
				
			||||||
        require_once $classFile;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -24,7 +24,8 @@ 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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -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';
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (class_exists($className) || Autoload::isLoadable($className)) {
 | 
					        if (!is_a($instance,'Mage\Task\AbstractTask')) {
 | 
				
			||||||
        	$instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
        	throw new ErrorWithMessageException('The Task "' . $taskName . '" doesn\'t exists.');
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!($instance instanceOf 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
									
									
									
									
									
								
							
							
						
						
									
										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…
	
		Reference in New Issue
	
	Block a user