From edebd4f638c3d60ce5a2329ff834c2f1048ef8be Mon Sep 17 00:00:00 2001 From: Matthew Gamble Date: Wed, 20 Feb 2019 15:39:34 +1100 Subject: [PATCH] Provide easy access to composer loader object Also includes some code style fixes and license header updates. --- README.md | 4 +-- composer.json | 4 +-- .../Fontis/ComposerAutoloader/Helper/Data.php | 34 ++++++++++++++++--- .../ComposerAutoloader/Model/Observer.php | 14 ++++---- .../Fontis/ComposerAutoloader/etc/config.xml | 8 ++--- src/app/code/community/Varien/Profiler.php | 19 +++++++++++ src/app/etc/fontis_composerautoloader.xml | 6 ++-- .../etc/modules/Fontis_ComposerAutoloader.xml | 6 ++-- 8 files changed, 70 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index edaec44..3cab394 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Installation "require" section: ``` - "fontis/composer-autoloader": "2.0.*" + "fontis/composer-autoloader": "2.1.*" ``` 2. Edit `app/etc/local.xml` and add the following XML. Modify the path to suit @@ -53,4 +53,4 @@ line versus how Magento does it for web requests. This is an improvement over v1 of this extension, where you had to manually dispatch an event after bootstrapping Magento to ensure the composer autoloader -was required. +was required in command-line scripts. diff --git a/composer.json b/composer.json index 61e4475..ffdba05 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "homepage": "http://www.fontis.com.au", "authors": [ { - "name":"Fontis", - "email":"magento@fontis.com.au" + "name": "Fontis", + "email": "magento@fontis.com.au" } ], "require": { diff --git a/src/app/code/community/Fontis/ComposerAutoloader/Helper/Data.php b/src/app/code/community/Fontis/ComposerAutoloader/Helper/Data.php index ee36ae0..3dd5bb2 100644 --- a/src/app/code/community/Fontis/ComposerAutoloader/Helper/Data.php +++ b/src/app/code/community/Fontis/ComposerAutoloader/Helper/Data.php @@ -7,14 +7,16 @@ * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: - * http://opensource.org/licenses/osl-3.0.php + * https://opensource.org/licenses/osl-3.0.php * * @category Fontis * @package Fontis_ComposerAutoloader - * @copyright Copyright (c) 2014 Fontis Pty. Ltd. (http://www.fontis.com.au) - * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @copyright Copyright (c) 2019 Fontis Pty. Ltd. (https://www.fontis.com.au) + * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ - + +use Composer\Autoload\ClassLoader; + class Fontis_ComposerAutoloader_Helper_Data extends Mage_Core_Helper_Abstract { const AUTOLOAD_FILENAME = 'autoload.php'; @@ -32,6 +34,7 @@ public function getVendorDirectoryPath() if (!$path) { $path = self::DEFAULT_PATH; } + $path = str_replace('/', DS, $path); $path = str_replace('{{basedir}}', Mage::getBaseDir(), $path); $path = str_replace('{{libdir}}', Mage::getBaseDir('lib'), $path); @@ -42,17 +45,38 @@ public function getVendorDirectoryPath() /** * @param string|null $path Path to vendor directory. Pass null to use the configured value. * @param string|null $filename Filename of autoload file. Pass null to use the default (autoload.php). + * @return bool */ public function registerAutoloader($path = null, $filename = null) + { + $loader = $this->getAutoloader($path, $filename); + if ($loader) { + return true; + } else { + return false; + } + } + + /** + * @param string|null $path Path to vendor directory. Pass null to use the configured value. + * @param string|null $filename Filename of autoload file. Pass null to use the default (autoload.php). + * @return ClassLoader|null + */ + public function getAutoloader($path = null, $filename = null) { if ($path === null) { $path = $this->getVendorDirectoryPath(); } + if ($filename === null) { $filename = self::AUTOLOAD_FILENAME; } + if (file_exists($path . DS . $filename)) { - require_once($path . DS . $filename); + // phpcs:ignore MEQP1.Security.IncludeFile.FoundIncludeFile -- No other alternative. + return require($path . DS . $filename); + } else { + return null; } } } diff --git a/src/app/code/community/Fontis/ComposerAutoloader/Model/Observer.php b/src/app/code/community/Fontis/ComposerAutoloader/Model/Observer.php index dc5c277..cc818d2 100644 --- a/src/app/code/community/Fontis/ComposerAutoloader/Model/Observer.php +++ b/src/app/code/community/Fontis/ComposerAutoloader/Model/Observer.php @@ -7,14 +7,14 @@ * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: - * http://opensource.org/licenses/osl-3.0.php + * https://opensource.org/licenses/osl-3.0.php * * @category Fontis * @package Fontis_ComposerAutoloader - * @copyright Copyright (c) 2014 Fontis Pty. Ltd. (http://www.fontis.com.au) - * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @copyright Copyright (c) 2019 Fontis Pty. Ltd. (https://www.fontis.com.au) + * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ - + class Fontis_ComposerAutoloader_Model_Observer { /** @@ -23,13 +23,15 @@ class Fontis_ComposerAutoloader_Model_Observer protected static $added = false; /** - * Register the Composer autoloader + * Register the Composer autoloader. + * + * @listen resource_get_tablename * @param Varien_Event_Observer $observer */ public function addComposerAutoloader(Varien_Event_Observer $observer) { if (self::$added === false) { - /** @var $helper Fontis_ComposerAutoloader_Helper_Data */ + /** @var Fontis_ComposerAutoloader_Helper_Data $helper */ $helper = Mage::helper('fontis_composerautoloader'); $helper->registerAutoloader(); self::$added = true; diff --git a/src/app/code/community/Fontis/ComposerAutoloader/etc/config.xml b/src/app/code/community/Fontis/ComposerAutoloader/etc/config.xml index 2f0f902..fada81b 100644 --- a/src/app/code/community/Fontis/ComposerAutoloader/etc/config.xml +++ b/src/app/code/community/Fontis/ComposerAutoloader/etc/config.xml @@ -8,18 +8,18 @@ * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: - * http://opensource.org/licenses/osl-3.0.php + * https://opensource.org/licenses/osl-3.0.php * * @category Fontis * @package Fontis_ComposerAutoloader - * @copyright Copyright (c) 2014 Fontis Pty. Ltd. (http://www.fontis.com.au) - * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @copyright Copyright (c) 2019 Fontis Pty. Ltd. (https://www.fontis.com.au) + * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ --> - 2.0.3 + 2.1.1 diff --git a/src/app/code/community/Varien/Profiler.php b/src/app/code/community/Varien/Profiler.php index 7b7eea5..d6a641b 100644 --- a/src/app/code/community/Varien/Profiler.php +++ b/src/app/code/community/Varien/Profiler.php @@ -1,4 +1,22 @@ diff --git a/src/app/etc/modules/Fontis_ComposerAutoloader.xml b/src/app/etc/modules/Fontis_ComposerAutoloader.xml index 614f134..eeac9a3 100644 --- a/src/app/etc/modules/Fontis_ComposerAutoloader.xml +++ b/src/app/etc/modules/Fontis_ComposerAutoloader.xml @@ -8,12 +8,12 @@ * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: - * http://opensource.org/licenses/osl-3.0.php + * https://opensource.org/licenses/osl-3.0.php * * @category Fontis * @package Fontis_ComposerAutoloader - * @copyright Copyright (c) 2014 Fontis Pty. Ltd. (http://www.fontis.com.au) - * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @copyright Copyright (c) 2019 Fontis Pty. Ltd. (https://www.fontis.com.au) + * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ -->