Skip to content

Commit 2b21a05

Browse files
authored
Merge pull request #3052 from mostafakhudair/patch-9
Autoload improvement
2 parents 1dfc79f + 03ccea1 commit 2b21a05

File tree

2 files changed

+86
-133
lines changed

2 files changed

+86
-133
lines changed

app/Config/Autoload.php

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace Config;
1+
<?php
2+
3+
namespace Config;
24

35
use CodeIgniter\Config\AutoloadConfig;
46

@@ -8,81 +10,57 @@
810
* -------------------------------------------------------------------
911
* This file defines the namespaces and class maps so the Autoloader
1012
* can find the files as needed.
13+
*
14+
* NOTE: If you use an identical key in $psr4 or $classmap, then
15+
* the values in this file will overwrite the framework's values.
1116
*/
1217
class Autoload extends AutoloadConfig
1318
{
14-
public $psr4 = [];
15-
16-
public $classmap = [];
17-
18-
//--------------------------------------------------------------------
1919

2020
/**
21-
* Collects the application-specific autoload settings and merges
22-
* them with the framework's required settings.
21+
* -------------------------------------------------------------------
22+
* Namespaces
23+
* -------------------------------------------------------------------
24+
* This maps the locations of any namespaces in your application to
25+
* their location on the file system. These are used by the autoloader
26+
* to locate files the first time they have been instantiated.
27+
*
28+
* The '/app' and '/system' directories are already mapped for you.
29+
* you may change the name of the 'App' namespace if you wish,
30+
* but this should be done prior to creating any namespaced classes,
31+
* else you will need to modify all of those classes for this to work.
2332
*
24-
* NOTE: If you use an identical key in $psr4 or $classmap, then
25-
* the values in this file will overwrite the framework's values.
33+
* Prototype:
34+
*
35+
* $psr4 = [
36+
* 'CodeIgniter' => SYSTEMPATH,
37+
* 'App' => APPPATH
38+
* ];
39+
*
40+
* @var array
2641
*/
27-
public function __construct()
28-
{
29-
parent::__construct();
30-
31-
/**
32-
* -------------------------------------------------------------------
33-
* Namespaces
34-
* -------------------------------------------------------------------
35-
* This maps the locations of any namespaces in your application
36-
* to their location on the file system. These are used by the
37-
* Autoloader to locate files the first time they have been instantiated.
38-
*
39-
* The '/app' and '/system' directories are already mapped for
40-
* you. You may change the name of the 'App' namespace if you wish,
41-
* but this should be done prior to creating any namespaced classes,
42-
* else you will need to modify all of those classes for this to work.
43-
*
44-
* DO NOT change the name of the CodeIgniter namespace or your application
45-
* WILL break. *
46-
* Prototype:
47-
*
48-
* $Config['psr4'] = [
49-
* 'CodeIgniter' => SYSPATH
50-
* `];
51-
*/
52-
$psr4 = [
53-
'App' => APPPATH, // To ensure filters, etc still found,
54-
APP_NAMESPACE => APPPATH, // For custom namespace
55-
'Config' => APPPATH . 'Config',
56-
];
57-
58-
/**
59-
* -------------------------------------------------------------------
60-
* Class Map
61-
* -------------------------------------------------------------------
62-
* The class map provides a map of class names and their exact
63-
* location on the drive. Classes loaded in this manner will have
64-
* slightly faster performance because they will not have to be
65-
* searched for within one or more directories as they would if they
66-
* were being autoloaded through a namespace.
67-
*
68-
* Prototype:
69-
*
70-
* $Config['classmap'] = [
71-
* 'MyClass' => '/path/to/class/file.php'
72-
* ];
73-
*/
74-
$classmap = [];
75-
76-
//--------------------------------------------------------------------
77-
// Do Not Edit Below This Line
78-
//--------------------------------------------------------------------
79-
80-
$this->psr4 = array_merge($this->psr4, $psr4);
81-
$this->classmap = array_merge($this->classmap, $classmap);
82-
83-
unset($psr4, $classmap);
84-
}
85-
86-
//--------------------------------------------------------------------
42+
public $psr4 = [
43+
APP_NAMESPACE => APPPATH, // For custom app namespace
44+
'Config' => APPPATH . 'Config',
45+
];
8746

47+
/**
48+
* -------------------------------------------------------------------
49+
* Class Map
50+
* -------------------------------------------------------------------
51+
* The class map provides a map of class names and their exact
52+
* location on the drive. Classes loaded in this manner will have
53+
* slightly faster performance because they will not have to be
54+
* searched for within one or more directories as they would if they
55+
* were being autoloaded through a namespace.
56+
*
57+
* Prototype:
58+
*
59+
* $classmap = [
60+
* 'MyClass' => '/path/to/class/file.php'
61+
* ];
62+
*
63+
* @var array
64+
*/
65+
public $classmap = [];
8866
}

system/Config/AutoloadConfig.php

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -49,90 +49,65 @@ class AutoloadConfig
4949
{
5050

5151
/**
52-
* Array of namespaces for autoloading.
52+
* -------------------------------------------------------------------
53+
* Namespaces
54+
* -------------------------------------------------------------------
55+
* This maps the locations of any namespaces in your application to
56+
* their location on the file system. These are used by the autoloader
57+
* to locate files the first time they have been instantiated.
58+
*
59+
* Do not change the name of the CodeIgniter namespace or your application
60+
* will break.
5361
*
5462
* @var array
5563
*/
56-
public $psr4 = [];
64+
protected $corePsr4 = [
65+
'CodeIgniter' => SYSTEMPATH,
66+
'App' => APPPATH // To ensure filters, etc still found,
67+
];
5768

5869
/**
59-
* Map of class names and locations
70+
* -------------------------------------------------------------------
71+
* Class Map
72+
* -------------------------------------------------------------------
73+
* The class map provides a map of class names and their exact
74+
* location on the drive. Classes loaded in this manner will have
75+
* slightly faster performance because they will not have to be
76+
* searched for within one or more directories as they would if they
77+
* were being autoloaded through a namespace.
6078
*
6179
* @var array
6280
*/
63-
public $classmap = [];
81+
protected $coreClassmap = [
82+
'Psr\Log\AbstractLogger' => SYSTEMPATH . 'ThirdParty/PSR/Log/AbstractLogger.php',
83+
'Psr\Log\InvalidArgumentException' => SYSTEMPATH . 'ThirdParty/PSR/Log/InvalidArgumentException.php',
84+
'Psr\Log\LoggerAwareInterface' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerAwareInterface.php',
85+
'Psr\Log\LoggerAwareTrait' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerAwareTrait.php',
86+
'Psr\Log\LoggerInterface' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerInterface.php',
87+
'Psr\Log\LoggerTrait' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerTrait.php',
88+
'Psr\Log\LogLevel' => SYSTEMPATH . 'ThirdParty/PSR/Log/LogLevel.php',
89+
'Psr\Log\NullLogger' => SYSTEMPATH . 'ThirdParty/PSR/Log/NullLogger.php',
90+
'Laminas\Escaper\Escaper' => SYSTEMPATH . 'ThirdParty/Escaper/Escaper.php'
91+
];
6492

6593
//--------------------------------------------------------------------
6694

6795
/**
6896
* Constructor.
97+
*
98+
* Merge the built-in and developer-configured psr4 and classmap,
99+
* with preference to the developer ones.
69100
*/
70101
public function __construct()
71102
{
72-
/**
73-
* -------------------------------------------------------------------
74-
* Namespaces
75-
* -------------------------------------------------------------------
76-
* This maps the locations of any namespaces in your application
77-
* to their location on the file system. These are used by the
78-
* Autoloader to locate files the first time they have been instantiated.
79-
*
80-
* The '/application' and '/system' directories are already mapped for
81-
* you. You may change the name of the 'App' namespace if you wish,
82-
* but this should be done prior to creating any namespaced classes,
83-
* else you will need to modify all of those classes for this to work.
84-
*
85-
* DO NOT change the name of the CodeIgniter namespace or your application
86-
* WILL break. *
87-
* Prototype:
88-
*
89-
* $Config['psr4'] = [
90-
* 'CodeIgniter' => SYSPATH
91-
* `];
92-
*/
93-
$this->psr4 = [
94-
'CodeIgniter' => realpath(SYSTEMPATH),
95-
];
96-
97103
if (isset($_SERVER['CI_ENVIRONMENT']) && $_SERVER['CI_ENVIRONMENT'] === 'testing')
98104
{
99105
$this->psr4['Tests\Support'] = SUPPORTPATH;
100-
}
101-
102-
/**
103-
* -------------------------------------------------------------------
104-
* Class Map
105-
* -------------------------------------------------------------------
106-
* The class map provides a map of class names and their exact
107-
* location on the drive. Classes loaded in this manner will have
108-
* slightly faster performance because they will not have to be
109-
* searched for within one or more directories as they would if they
110-
* were being autoloaded through a namespace.
111-
*
112-
* Prototype:
113-
*
114-
* $Config['classmap'] = [
115-
* 'MyClass' => '/path/to/class/file.php'
116-
* ];
117-
*/
118-
$this->classmap = [
119-
'Psr\Log\AbstractLogger' => SYSTEMPATH . 'ThirdParty/PSR/Log/AbstractLogger.php',
120-
'Psr\Log\InvalidArgumentException' => SYSTEMPATH . 'ThirdParty/PSR/Log/InvalidArgumentException.php',
121-
'Psr\Log\LoggerAwareInterface' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerAwareInterface.php',
122-
'Psr\Log\LoggerAwareTrait' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerAwareTrait.php',
123-
'Psr\Log\LoggerInterface' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerInterface.php',
124-
'Psr\Log\LoggerTrait' => SYSTEMPATH . 'ThirdParty/PSR/Log/LoggerTrait.php',
125-
'Psr\Log\LogLevel' => SYSTEMPATH . 'ThirdParty/PSR/Log/LogLevel.php',
126-
'Psr\Log\NullLogger' => SYSTEMPATH . 'ThirdParty/PSR/Log/NullLogger.php',
127-
'Laminas\Escaper\Escaper' => SYSTEMPATH . 'ThirdParty/Escaper/Escaper.php',
128-
];
129-
130-
if (isset($_SERVER['CI_ENVIRONMENT']) && $_SERVER['CI_ENVIRONMENT'] === 'testing')
131-
{
132106
$this->classmap['CodeIgniter\Log\TestLogger'] = SYSTEMPATH . 'Test/TestLogger.php';
133107
$this->classmap['CIDatabaseTestCase'] = SYSTEMPATH . 'Test/CIDatabaseTestCase.php';
134108
}
135-
}
136109

137-
//--------------------------------------------------------------------
110+
$this->psr4 = array_merge($this->corePsr4, $this->psr4);
111+
$this->classmap = array_merge($this->coreClassmap, $this->classmap);
112+
}
138113
}

0 commit comments

Comments
 (0)