Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composer plugin v3 #63

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
<ruleset name="OpenMage" namespace="OpenMage\CS\Standard">
<config name="testVersion" value="8.2-"/>
<file>maho</file>
<file>app/bootstrap.php</file>
<file>app/Mage.php</file>
<file>app/code/core/Mage/</file>
<file>lib/Mage/</file>
<file>lib/MahoCLI/</file>
<file>lib/Varien/</file>
<file>app</file>
<file>lib</file>
<file>public/api.php</file>
<file>public/index.php</file>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>public/api.php</exclude-pattern>
<exclude-pattern>public/index.php</exclude-pattern>
<exclude-pattern>app/Mage.php</exclude-pattern>
<exclude-pattern>app/bootstrap.php</exclude-pattern>
</rule>
<rule ref="PSR2">
<exclude name="Generic.Files.LineLength.TooLong"/>
Expand Down
60 changes: 5 additions & 55 deletions app/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,6 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
define('BP', MAHO_ROOT_DIR);

/*
* Require Composer autoloader and set include paths
* @var \Composer\Autoload\ClassLoader $composerClassLoader
*/
$composerClassLoader = require BP . '/vendor/autoload.php';
set_include_path(implode(PS, \Maho\MahoAutoload::generatePaths(BP)) . PS . get_include_path());

if (!empty($_SERVER['MAGE_IS_DEVELOPER_MODE']) || !empty($_ENV['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
ini_set('display_errors', '1');
ini_set('error_prepend_string', '<pre>');
ini_set('error_append_string', '</pre>');

// Fix for overriding zf1-future during development
ini_set('opcache.revalidate_path', 1);

// Check if we used `composer dump --optimize-autoloader` in development
$classMap = $composerClassLoader->getClassMap();
if (isset($classMap['Mage_Core_Model_App'])) {
Mage::addBootupWarning('Optimized autoloader detected in developer mode.');
}

// Reload PSR-0 namespaces and controller classmap during development in case new files are added
$prefixes = $composerClassLoader->getPrefixes();
foreach (\Maho\MahoAutoload::generatePsr0(BP) as $prefix => $paths) {
$prefixes[$prefix] ??= [];
if (count($prefixes[$prefix])) {
$prefixes[$prefix] = array_diff($prefixes[$prefix], $paths);
}
array_push($prefixes[$prefix], ...$paths);
$composerClassLoader->set($prefix, $paths);
}
$composerClassLoader->addClassMap(\Maho\MahoAutoload::generateControllerClassMap(BP));
}

require_once __DIR__ . '/code/core/Mage/Core/functions.php';

/**
* Support additional includes, originally used for OpenMage composer support
* See: https://github.com/OpenMage/magento-lts/pull/559
*/
foreach (glob(BP . '/app/etc/includes/*.php') as $path) {
include_once $path;
}

/**
* Main Mage hub class
*/
Expand Down Expand Up @@ -588,9 +539,8 @@ public static function throwException($message, $messageStorage = null)

public static function addBootupWarning(string $message)
{
$messages = Mage::registry('bootup_warnings') ?? [];
$messages[] = $message;
Mage::register('bootup_warnings', $message);
self::$_registry['bootup_warnings'] ??= [];
self::$_registry['bootup_warnings'][] = $message;
}

/**
Expand Down Expand Up @@ -642,7 +592,7 @@ public static function init($code = '', $type = 'store', $options = [], $modules
header('Location: ' . self::getBaseUrl());
die;
} catch (Mage_Core_Model_Store_Exception $e) {
mahoErrorReport([], 404);
Maho::errorReport([], 404);
die;
} catch (Exception $e) {
self::printException($e);
Expand Down Expand Up @@ -685,7 +635,7 @@ public static function run($code = '', $type = 'store', $options = [])
header('Location: ' . self::getBaseUrl());
die();
} catch (Mage_Core_Model_Store_Exception $e) {
mahoErrorReport([], 404);
Maho::errorReport([], 404);
die();
} catch (Exception $e) {
if (self::isInstalled()) {
Expand Down Expand Up @@ -926,7 +876,7 @@ public static function printException(Throwable $e, $extra = '')
$reportData['script_name'] = $_SERVER['SCRIPT_NAME'];
}

mahoErrorReport($reportData);
Maho::errorReport($reportData);
}

die();
Expand Down
43 changes: 36 additions & 7 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,43 @@
* @package Mage
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
* @copyright Copyright (c) 2020-2022 The OpenMage Contributors (https://openmage.org)
* @copyright Copyright (c) 2024 Maho (https://mahocommerce.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Apply workaround for the libxml PHP bugs:
* @link https://bugs.php.net/bug.php?id=62577
* @link https://bugs.php.net/bug.php?id=64938
*/
if ((LIBXML_VERSION < 20900) && function_exists('libxml_disable_entity_loader')) {
libxml_disable_entity_loader(false);
// Require the autoloader if not already loaded
if (!class_exists('Mage')) {
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
require __DIR__ . '/../../../autoload.php';
} else {
throw new Exception('Autoloader not found. Please run \'composer install\'.');
}
}

defined('DS') || define('DS', DIRECTORY_SEPARATOR);
defined('PS') || define('PS', PATH_SEPARATOR);
defined('BP') || define('BP', Maho::getBasePath());

/** @deprecated */
defined('MAGENTO_ROOT') || define('MAGENTO_ROOT', BP);

if (!empty($_SERVER['MAGE_IS_DEVELOPER_MODE']) || !empty($_ENV['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);

ini_set('display_errors', '1');
ini_set('error_prepend_string', '<pre>');
ini_set('error_append_string', '</pre>');

// Fix for overriding zf1-future during development
ini_set('opcache.revalidate_path', 1);

// Update Composer's autoloader during development in case new files are added
Maho::updateComposerAutoloader();

// Check if we used `composer dump --optimize-autoloader` in development
if (Maho::isComposerAutoloaderOptimized()) {
Mage::addBootupWarning('Optimized autoloader detected in developer mode.');
}
}
8 changes: 4 additions & 4 deletions app/code/core/Mage/Api/Model/Wsdl/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ public function init()
* Exclude Mage_Api wsdl xml file because it used for previous version
* of API wsdl declaration
*/
$mergeWsdl->addLoadedFile(mahoFindFileInIncludePath("$moduleDir/wsi.xml"));
$mergeWsdl->addLoadedFile(Maho::findFile("$moduleDir/wsi.xml"));

// Base wsi file
$this->loadFile(mahoFindFileInIncludePath("$moduleDir/wsi.xml"));
$this->loadFile(Maho::findFile("$moduleDir/wsi.xml"));

Mage::getConfig()->loadModulesConfiguration('wsi.xml', $this, $mergeWsdl);
} else {
/**
* Exclude Mage_Api wsdl xml file because it used for previous version
* of API wsdl declaration
*/
$mergeWsdl->addLoadedFile(mahoFindFileInIncludePath("$moduleDir/wsdl.xml"));
$mergeWsdl->addLoadedFile(Maho::findFile("$moduleDir/wsdl.xml"));

// Base wsdl file
$this->loadFile(mahoFindFileInIncludePath("$moduleDir/wsdl2.xml"));
$this->loadFile(Maho::findFile("$moduleDir/wsdl2.xml"));

Mage::getConfig()->loadModulesConfiguration('wsdl.xml', $this, $mergeWsdl);
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Core/Block/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public function fetchView($fileName)
&&
($this->_viewDir == Mage::getBaseDir('design') || str_starts_with(realpath($this->_viewDir), realpath(Mage::getBaseDir('design'))))
) {
$fileToInclude = mahoFindFileInIncludePath($fileName);
$fileToInclude = Maho::findFile($fileName);
if (!$fileToInclude) {
$fileToInclude = mahoFindFileInIncludePath($this->_viewDir . DS . $fileName);
$fileToInclude = Maho::findFile($this->_viewDir . DS . $fileName);
}
include $fileToInclude;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public function getControllerFileName($realModule, $controller)
$file .= DS . implode(DS, $parts);
}
$file .= DS . uc_words($controller, DS) . 'Controller.php';
$file = mahoFindFileInIncludePath($file);
$file = Maho::findFile($file);
return $file;
}

Expand Down
74 changes: 33 additions & 41 deletions app/code/core/Mage/Core/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Mage_Core_Model_Config extends Mage_Core_Model_Config_Base
/**
* Configuration xml
*
* @var Mage_Core_Model_Config_Element
* @var Mage_Core_Model_Config_Element|null
*/
protected $_xml = null;

Expand Down Expand Up @@ -206,6 +206,11 @@ class Mage_Core_Model_Config extends Mage_Core_Model_Config_Base
*/
protected $_prototype;

/**
* Reference to the Varien_Simplexml_Config object where local.xml was loaded in to
*/
protected Mage_Core_Model_Config_Base $_refLocalConfigObject;

/**
* Flag which identify what local configuration is loaded
*
Expand Down Expand Up @@ -323,36 +328,35 @@ public function init($options = [])
*/
public function loadBase()
{
// Prevent double loading of base config
if ($this->getNode() !== false) {
return $this;
}

$files = [];

// Include Maho core and 3rd party module files
$modules = \Maho\MahoAutoload::getInstalledModules(BP);
foreach ($modules as $module => $info) {
foreach (Maho::getInstalledPackages() as $package => $info) {
foreach (glob($info['path'] . '/app/etc/*.xml') as $file) {
$files[basename($file)] = $file;
$basename = basename($file);
if ($basename === 'local.xml' && $package !== 'root') {
continue;
}
$files[$basename] = $file;
}
}

// Prevent any module from defining a local.xml
unset($files['local.xml']);

// Include local files, overriding core and 3rd party module files
foreach (glob($this->getOptions()->getEtcDir() . '/*.xml') as $file) {
$files[basename($file)] = $file;
}

// Merge all config files
$this->loadFile(current($files));
while ($file = next($files)) {
$this->loadString('<?xml version="1.0"?><config></config>');
foreach ($files as $basename => $file) {
$merge = clone $this->_prototype;
$merge->loadFile($file);
if ($basename === 'local.xml') {
$this->_isLocalConfigLoaded = true;
$this->_refLocalConfigObject = $merge;
}
$this->extend($merge);
}

if (isset($files['local.xml'])) {
$this->_isLocalConfigLoaded = true;
}

return $this;
}

Expand Down Expand Up @@ -388,13 +392,9 @@ public function loadModules()
$resourceConfig = sprintf('config.%s.xml', $this->_getResourceConnectionModel('core'));
$this->loadModulesConfiguration(['config.xml', $resourceConfig], $this);

/**
* Prevent local.xml directives overwriting
*/
$mergeConfig = clone $this->_prototype;
$this->_isLocalConfigLoaded = $mergeConfig->loadFile($this->getOptions()->getEtcDir() . DS . 'local.xml');
// Prevent local.xml directives overwriting
if ($this->_isLocalConfigLoaded) {
$this->extend($mergeConfig);
$this->extend($this->_refLocalConfigObject);
}

$this->applyExtends();
Expand Down Expand Up @@ -451,6 +451,8 @@ public function loadEnv(): Mage_Core_Model_Config
*/
public function reinit($options = [])
{
$this->_xml = null;
$this->_isLocalConfigLoaded = false;
$this->_allowCacheForInit = false;
$this->_useCache = false;
return $this->init($options);
Expand Down Expand Up @@ -503,7 +505,7 @@ public function getCacheSaveLock($waitTime = null, $ignoreFailure = false)
throw new Exception('Could not get lock on cache save operation.');
} else {
Mage::log(sprintf('Failed to get cache save lock in %d seconds.', $waitTime), Zend_Log::NOTICE);
mahoErrorReport();
Maho::errorReport();
die();
}
}
Expand Down Expand Up @@ -780,20 +782,11 @@ protected function _getDeclaredModuleFiles()
{
$moduleFiles = [];

// Include Maho core and 3rd party module files
$modules = \Maho\MahoAutoload::getInstalledModules(BP);
foreach ($modules as $module => $info) {
foreach (glob($info['path'] . '/app/etc/modules/*.xml') as $file) {
$moduleFiles[basename($file)] = $file;
}
}

// Include local files, overriding core and 3rd party module files
foreach (glob($this->getOptions()->getEtcDir() . '/modules/*.xml') as $file) {
foreach (Maho::globPackages('/app/etc/modules/*.xml') as $file) {
$moduleFiles[basename($file)] = $file;
}

if (!$moduleFiles) {
if (empty($moduleFiles)) {
return false;
}

Expand All @@ -803,9 +796,7 @@ protected function _getDeclaredModuleFiles()
];

foreach ($moduleFiles as $v) {
$name = explode(DIRECTORY_SEPARATOR, $v);
$name = substr($name[count($name) - 1], 0, -4);

$name = pathinfo($v, PATHINFO_FILENAME);
if (array_key_exists($name, self::MAGE_MODULES)) {
$collectModuleFiles['mage'][self::MAGE_MODULES[$name]] = $v;
} else {
Expand Down Expand Up @@ -1037,6 +1028,7 @@ public function loadModulesConfiguration($fileName, $mergeToObject = null, $merg
if ($mergeModel === null) {
$mergeModel = clone $this->_prototype;
}

$modules = $this->getNode('modules')->children();
foreach ($modules as $modName => $module) {
if ($module->is('active')) {
Expand All @@ -1046,7 +1038,7 @@ public function loadModulesConfiguration($fileName, $mergeToObject = null, $merg

foreach ($fileName as $configFile) {
$moduleDir = $this->getModuleDir('etc', $modName);
$configFile = mahoFindFileInIncludePath("$moduleDir/$configFile");
$configFile = Maho::findFile("$moduleDir/$configFile");

if ($mergeModel->loadFile($configFile)) {
$this->_makeEventsLowerCase(Mage_Core_Model_App_Area::AREA_GLOBAL, $mergeModel);
Expand Down
4 changes: 1 addition & 3 deletions app/code/core/Mage/Core/Model/Config/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class Mage_Core_Model_Config_System extends Mage_Core_Model_Config_Base
*/
public function load($module)
{
$file = Mage::getConfig()->getModuleDir('etc', $module) . DS . 'system.xml';
$file = mahoFindFileInIncludePath($file);
$this->loadFile($file);
$this->loadFile(Maho::findFile(Mage::getConfig()->getModuleDir('etc', $module) . '/system.xml'));
return $this;
}
}
Loading
Loading