From 9af42046dbcc6449c0cd4f5265f917280a54379a Mon Sep 17 00:00:00 2001 From: Kuba Turek Date: Wed, 27 Aug 2014 21:09:59 +0200 Subject: [PATCH 1/6] Add "vendor" to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 458da2e..5dc363a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .project .buildpath .idea +vendor # OS generated files # // GitHub Recommendation ###################### From 300cc4c56f34925ca8105924743b243898243b8c Mon Sep 17 00:00:00 2001 From: Kuba Turek Date: Thu, 28 Aug 2014 21:28:30 +0200 Subject: [PATCH 2/6] Load core classes wit hcomposer autoloader --- bin/mage | 7 +------ composer.json | 5 +++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/mage b/bin/mage index 9f6e436..ea8f612 100755 --- a/bin/mage +++ b/bin/mage @@ -9,8 +9,6 @@ * file that was distributed with this source code. */ -use Mage\Autoload; - date_default_timezone_set('UTC'); $baseDir = dirname(dirname(__FILE__)); @@ -18,10 +16,7 @@ $baseDir = dirname(dirname(__FILE__)); define('MAGALLANES_VERSION', '1.0.1'); define('MAGALLANES_DIRECTORY', $baseDir); -// Preload -require_once $baseDir . '/Mage/Autoload.php'; -$loader = new Autoload(); -spl_autoload_register(array($loader, 'autoLoad')); +require_once __DIR__ . '/../vendor/autoload.php'; // Clean arguments array_shift($argv); diff --git a/composer.json b/composer.json index c533d31..90be3b2 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,11 @@ "require": { "php": ">=5.3" }, + "autoload": { + "psr-4": { + "Mage\\": "./Mage" + } + }, "bin": [ "bin/mage" ] From ea49214e952640d1d712e4ffba908509969033c9 Mon Sep 17 00:00:00 2001 From: Kuba Turek Date: Thu, 28 Aug 2014 21:34:07 +0200 Subject: [PATCH 3/6] Load custom tasks and commands by composer autoload mechanism --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 90be3b2..4018174 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,9 @@ }, "autoload": { "psr-4": { - "Mage\\": "./Mage" + "Mage\\": "./Mage", + "Task\\": ".mage/tasks", + "Command\\": ".mage/commands" } }, "bin": [ From 07326efbe2b77ca77e569718ae8c618add359d32 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 7 Sep 2014 10:49:50 +0200 Subject: [PATCH 4/6] Remove old Autoload class --- Mage/Autoload.php | 72 ----------------------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 Mage/Autoload.php diff --git a/Mage/Autoload.php b/Mage/Autoload.php deleted file mode 100644 index 474aa0c..0000000 --- a/Mage/Autoload.php +++ /dev/null @@ -1,72 +0,0 @@ - -* -* For the full copyright and license information, please view the LICENSE -* file that was distributed with this source code. -*/ - -namespace Mage; - -/** - * Magallanes custom Autoload for BuiltIn and Userspace Commands and Tasks. - * - * @author Andrés Montañez - */ -class Autoload -{ - /** - * Autoload a Class by it's Class Name - * @param string $className - * @return boolean - */ - public function autoLoad($className) - { - $className = ltrim($className, '/'); - $postfix = '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - - // Change BaseDir according to Namespace - if (strpos($className, 'Task\\') === 0) { - $baseDir = getcwd() . '/.mage/tasks'; - $postfix = substr($postfix, 5); - - } else if (strpos($className, 'Command\\') === 0) { - $baseDir = getcwd() . '/.mage/commands'; - $postfix = substr($postfix, 8); - - } else { - $baseDir = dirname(dirname(__FILE__)); - } - - //Try to load a normal Mage class (or Task). Think that Mage component is compiled to .phar - $classFileWithinPhar = $baseDir . $postfix; - if ($this->isReadable($classFileWithinPhar)) { - /** @noinspection PhpIncludeInspection */ - 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)) { - /** @noinspection PhpIncludeInspection */ - require_once $classFileOutsidePhar; - return true; - } - - return false; - } - - /** - * Checks if a file can be read. - * @param string $filePath - * @return boolean - */ - public function isReadable($filePath) - { - return is_readable($filePath); - } - -} From 5809a424a732f7dde55bebddd64b0f06b288f2d5 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 13 Sep 2014 21:24:26 +0200 Subject: [PATCH 5/6] Revert "Remove old Autoload class" This reverts commit 3f3253bbee3493c71e09a3074fd28a9d4485fc6c. --- Mage/Autoload.php | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Mage/Autoload.php diff --git a/Mage/Autoload.php b/Mage/Autoload.php new file mode 100644 index 0000000..474aa0c --- /dev/null +++ b/Mage/Autoload.php @@ -0,0 +1,72 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Mage; + +/** + * Magallanes custom Autoload for BuiltIn and Userspace Commands and Tasks. + * + * @author Andrés Montañez + */ +class Autoload +{ + /** + * Autoload a Class by it's Class Name + * @param string $className + * @return boolean + */ + public function autoLoad($className) + { + $className = ltrim($className, '/'); + $postfix = '/' . str_replace(array('_', '\\'), '/', $className . '.php'); + + // Change BaseDir according to Namespace + if (strpos($className, 'Task\\') === 0) { + $baseDir = getcwd() . '/.mage/tasks'; + $postfix = substr($postfix, 5); + + } else if (strpos($className, 'Command\\') === 0) { + $baseDir = getcwd() . '/.mage/commands'; + $postfix = substr($postfix, 8); + + } else { + $baseDir = dirname(dirname(__FILE__)); + } + + //Try to load a normal Mage class (or Task). Think that Mage component is compiled to .phar + $classFileWithinPhar = $baseDir . $postfix; + if ($this->isReadable($classFileWithinPhar)) { + /** @noinspection PhpIncludeInspection */ + 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)) { + /** @noinspection PhpIncludeInspection */ + require_once $classFileOutsidePhar; + return true; + } + + return false; + } + + /** + * Checks if a file can be read. + * @param string $filePath + * @return boolean + */ + public function isReadable($filePath) + { + return is_readable($filePath); + } + +} From bc428f466acbc0829bcc21d6ba2c55a4544ff2d9 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 13 Sep 2014 21:29:40 +0200 Subject: [PATCH 6/6] Use default autoloader when composer autoloader has been not initialized --- bin/mage | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/mage b/bin/mage index ea8f612..fcad36f 100755 --- a/bin/mage +++ b/bin/mage @@ -16,7 +16,14 @@ $baseDir = dirname(dirname(__FILE__)); define('MAGALLANES_VERSION', '1.0.1'); define('MAGALLANES_DIRECTORY', $baseDir); -require_once __DIR__ . '/../vendor/autoload.php'; +if (file_exists(__DIR__ . '/../vendor/autoload.php')) { + require_once __DIR__ . '/../vendor/autoload.php'; +} else { + require_once $baseDir . '/Mage/Autoload.php'; + $loader = new \Mage\Autoload(); + spl_autoload_register(array($loader, 'autoLoad')); +} + // Clean arguments array_shift($argv);