Skip to content

Commit 529d71a

Browse files
committed
Add autoload package
1 parent ebc9f45 commit 529d71a

File tree

10 files changed

+93
-85
lines changed

10 files changed

+93
-85
lines changed

autoload.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
* @see composer.json "files" property for custom autoloading
3030
*/
3131
require_once __DIR__.'/config/defines.inc.php';
32-
require_once __DIR__.'/classes/PrestaShopAutoload.php';
32+
require_once __DIR__.'/config/autoload.php';

classes/PrestaShopAutoload.php

+15-68
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
2525
*/
2626

27+
use PrestaShop\Autoload\LegacyClassLoader;
28+
29+
@trigger_error('Using PrestaShopAutoload is deprecated, use Prestashop\Autoload\PrestaShopAutoload instead', E_USER_DEPRECATED);
30+
2731
/**
2832
* Class PrestaShopAutoload.
2933
*
@@ -48,6 +52,11 @@ class PrestaShopAutoload
4852

4953
public $_include_override_path = true;
5054

55+
/**
56+
* @var LegacyClassLoader
57+
*/
58+
protected $classLoader;
59+
5160
protected static $class_aliases = [
5261
'Collection' => 'PrestaShopCollection',
5362
'Autoload' => 'PrestaShopAutoload',
@@ -58,7 +67,8 @@ class PrestaShopAutoload
5867
protected function __construct()
5968
{
6069
$this->root_dir = _PS_CORE_DIR_ . '/';
61-
$file = static::getCacheFileIndex();
70+
$this->classLoader = new LegacyClassLoader(_PS_ROOT_DIR_, _PS_CACHE_DIR_);
71+
$file = $this->classLoader->getClassIndexFilepath();
6272
$stubFile = static::getStubFileIndex();
6373
if (@filemtime($file) && is_readable($file) && @filemtime($stubFile) && is_readable($stubFile)) {
6474
$this->index = include $file;
@@ -154,7 +164,9 @@ public function load($className)
154164
require_once $this->root_dir . $this->index[$className]['path'];
155165
}
156166
if (strpos($className, 'PrestaShop\PrestaShop\Adapter\Entity') !== false) {
157-
require_once static::getNamespacedStubFileIndex();
167+
$legacyClass = substr($className, 37);
168+
$this->load($legacyClass);
169+
class_alias($legacyClass, '\\' . $className);
158170
}
159171
}
160172

@@ -172,70 +184,7 @@ public function generateIndex()
172184
}
173185
}
174186

175-
$coreClasses = $this->getClassesFromDir('classes/');
176-
177-
$classes = array_merge(
178-
$coreClasses,
179-
$this->getClassesFromDir('controllers/')
180-
);
181-
182-
$contentNamespacedStub = '<?php ' . "\n" . 'namespace PrestaShop\\PrestaShop\\Adapter\\Entity;' . "\n\n";
183-
184-
foreach ($coreClasses as $coreClassName => $coreClass) {
185-
if (substr($coreClassName, -4) == 'Core') {
186-
$coreClassName = substr($coreClassName, 0, -4);
187-
if ($coreClass['type'] != 'interface') {
188-
$contentNamespacedStub .= $coreClass['type'] . ' ' . $coreClassName . ' extends \\' . $coreClassName . ' {};' . "\n";
189-
}
190-
}
191-
}
192-
193-
if ($this->_include_override_path) {
194-
$coreOverrideClasses = $this->getClassesFromDir('override/classes/');
195-
$coreClassesWOOverrides = array_diff_key($coreClasses, $coreOverrideClasses);
196-
197-
$classes = array_merge(
198-
$classes,
199-
$coreOverrideClasses,
200-
$this->getClassesFromDir('override/controllers/')
201-
);
202-
} else {
203-
$coreClassesWOOverrides = $coreClasses;
204-
}
205-
206-
$contentStub = '<?php' . "\n\n";
207-
208-
foreach ($coreClassesWOOverrides as $coreClassName => $coreClass) {
209-
if (substr($coreClassName, -4) == 'Core') {
210-
$coreClassNameNoCore = substr($coreClassName, 0, -4);
211-
if ($coreClass['type'] != 'interface') {
212-
$contentStub .= $coreClass['type'] . ' ' . $coreClassNameNoCore . ' extends ' . $coreClassName . ' {};' . "\n";
213-
}
214-
}
215-
}
216-
217-
ksort($classes);
218-
$content = '<?php return ' . var_export($classes, true) . '; ?>';
219-
220-
// Write classes index on disc to cache it
221-
$filename = static::getCacheFileIndex();
222-
@mkdir(_PS_CACHE_DIR_, 0777, true);
223-
224-
if (!$this->dumpFile($filename, $content)) {
225-
Tools::error_log('Cannot write temporary file ' . $filename);
226-
}
227-
228-
$stubFilename = static::getStubFileIndex();
229-
if (!$this->dumpFile($stubFilename, $contentStub)) {
230-
Tools::error_log('Cannot write temporary file ' . $stubFilename);
231-
}
232-
233-
$namespacedStubFilename = static::getNamespacedStubFileIndex();
234-
if (!$this->dumpFile($namespacedStubFilename, $contentNamespacedStub)) {
235-
Tools::error_log('Cannot write temporary file ' . $namespacedStubFilename);
236-
}
237-
238-
$this->index = $classes;
187+
$this->index = $this->classLoader->buildClassIndex($this->_include_override_path);
239188
}
240189

241190
/**
@@ -330,5 +279,3 @@ public function getClassPath($classname)
330279
return (isset($this->index[$classname]['path'])) ? $this->index[$classname]['path'] : null;
331280
}
332281
}
333-
334-
spl_autoload_register([PrestaShopAutoload::getInstance(), 'load']);

classes/Tools.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2813,7 +2813,15 @@ public static function getRobotsContent()
28132813

28142814
public static function generateIndex()
28152815
{
2816-
PrestaShopAutoload::getInstance()->generateIndex();
2816+
@trigger_error(
2817+
sprintf(
2818+
'%s is deprecated since version 8.1. Use PrestaShop\Autoload\PrestashopAutoload instead.',
2819+
__METHOD__
2820+
),
2821+
E_USER_DEPRECATED
2822+
);
2823+
2824+
\PrestaShop\Autoload\PrestashopAutoload::getInstance()->generateIndex();
28172825
}
28182826

28192827
/**

classes/module/Module.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
2525
*/
2626

27+
use PrestaShop\Autoload\PrestashopAutoload;
2728
use PrestaShop\PrestaShop\Adapter\ContainerFinder;
2829
use PrestaShop\PrestaShop\Adapter\LegacyLogger;
2930
use PrestaShop\PrestaShop\Adapter\Module\ModuleDataProvider;
@@ -2997,7 +2998,7 @@ public function addOverride($classname)
29972998
file_put_contents($override_dest, preg_replace($pattern_escape_com, '', $module_file));
29982999

29993000
// Re-generate the class index
3000-
Tools::generateIndex();
3001+
PrestashopAutoload::getInstance()->generateIndex();
30013002
}
30023003

30033004
return true;
@@ -3244,7 +3245,7 @@ public function removeOverride($classname)
32443245
}
32453246

32463247
// Re-generate the class index
3247-
Tools::generateIndex();
3248+
PrestashopAutoload::getInstance()->generateIndex();
32483249

32493250
return true;
32503251
}

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"pear/archive_tar": "^1.4.12",
5959
"pelago/emogrifier": "^v5.0.1",
6060
"phpoffice/phpspreadsheet": "^1.19",
61+
"prestashop/autoload": "^1.0",
6162
"prestashop/blockreassurance": "^5.1",
6263
"prestashop/blockwishlist": "^2.0",
6364
"prestashop/circuit-breaker": "^4.0",
@@ -206,6 +207,10 @@
206207
{
207208
"type": "vcs",
208209
"url": "https://github.com/atomiix/jwt"
210+
},
211+
{
212+
"type": "vcs",
213+
"url": "https://github.com/PrestaShop/autoload"
209214
}
210215
],
211216
"minimum-stability": "dev",

composer.lock

+51-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/autoload.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
2525
*/
2626

27+
use PrestaShop\Autoload\PrestashopAutoload;
2728
use PrestaShop\PrestaShop\Core\Version;
2829

29-
require_once __DIR__.'/../vendor/autoload.php';
30+
require_once __DIR__ . '/../vendor/autoload.php';
3031

3132
define('_PS_VERSION_', Version::VERSION);
3233

33-
require_once _PS_CONFIG_DIR_.'alias.php';
34-
require_once _PS_CLASS_DIR_.'PrestaShopAutoload.php';
35-
spl_autoload_register(array(PrestaShopAutoload::getInstance(), 'load'));
34+
require_once _PS_CONFIG_DIR_ . 'alias.php';
35+
36+
PrestashopAutoload::create(_PS_ROOT_DIR_, _PS_CACHE_DIR_)
37+
->register();

controllers/admin/AdminTranslationsController.php

-5
Original file line numberDiff line numberDiff line change
@@ -2313,11 +2313,6 @@ public function initFormFields()
23132313
}
23142314

23152315
$class_name = substr($file, 0, -4);
2316-
2317-
if (!class_exists($class_name, false) && !class_exists($class_name . 'Core', false)) {
2318-
PrestaShopAutoload::getInstance()->load($class_name);
2319-
}
2320-
23212316
if (!is_subclass_of($class_name . 'Core', 'ObjectModel')) {
23222317
continue;
23232318
}

install-dev/classes/controllerHttp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ final public static function execute()
193193

194194
$session = InstallSession::getInstance();
195195
if (!$session->last_step || $session->last_step === 'welcome') {
196-
Tools::generateIndex();
196+
\PrestaShop\Autoload\PrestashopAutoload::getInstance()->generateIndex();
197197
}
198198

199199
if (empty($session->last_step)) {

src/Adapter/Cache/Clearer/ClassIndexCacheClearer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
namespace PrestaShop\PrestaShop\Adapter\Cache\Clearer;
2828

29+
use PrestaShop\Autoload\PrestashopAutoload;
2930
use PrestaShop\PrestaShop\Core\Cache\Clearer\CacheClearerInterface;
30-
use PrestaShopAutoload;
3131

3232
/**
3333
* Class ClassIndexCacheClearer clears current class index and generates new one.
@@ -41,6 +41,6 @@ final class ClassIndexCacheClearer implements CacheClearerInterface
4141
*/
4242
public function clear()
4343
{
44-
PrestaShopAutoload::getInstance()->generateIndex();
44+
PrestashopAutoload::getInstance()->generateIndex();
4545
}
4646
}

0 commit comments

Comments
 (0)