2 |
3 | # Aplus Framework Autoload Library
4 |
5 | - [Home](https://aplus-framework.com/packages/autoload)
6 | - [User Guide](https://docs.aplus-framework.com/guides/libraries/autoload/index.html)
7 | - [API Documentation](https://docs.aplus-framework.com/packages/autoload.html)
8 |
9 | [](https://github.com/aplus-framework/autoload/actions/workflows/tests.yml)
10 | [](https://coveralls.io/github/aplus-framework/autoload?branch=master)
11 | [](https://packagist.org/packages/aplus/autoload)
12 | [](https://aplus-framework.com/sponsor)
13 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Natan Felles
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "aplus/autoload",
3 | "description": "Aplus Framework Autoload Library",
4 | "license": "MIT",
5 | "type": "library",
6 | "keywords": [
7 | "autoload",
8 | "autoloader",
9 | "loader",
10 | "locator",
11 | "file-locator",
12 | "class-locator"
13 | ],
14 | "authors": [
15 | {
16 | "name": "Natan Felles",
17 | "email": "natanfelles@gmail.com",
18 | "homepage": "https://natanfelles.github.io"
19 | }
20 | ],
21 | "homepage": "https://aplus-framework.com/packages/autoload",
22 | "support": {
23 | "email": "support@aplus-framework.com",
24 | "issues": "https://github.com/aplus-framework/autoload/issues",
25 | "forum": "https://aplus-framework.com/forum",
26 | "source": "https://github.com/aplus-framework/autoload",
27 | "docs": "https://docs.aplus-framework.com/guides/libraries/autoload/"
28 | },
29 | "funding": [
30 | {
31 | "type": "Aplus Sponsor",
32 | "url": "https://aplus-framework.com/sponsor"
33 | }
34 | ],
35 | "require": {
36 | "php": ">=8.3",
37 | "ext-intl": "*",
38 | "aplus/debug": "^4.3"
39 | },
40 | "require-dev": {
41 | "ext-xdebug": "*",
42 | "aplus/coding-standard": "^2.8",
43 | "ergebnis/composer-normalize": "^2.25",
44 | "jetbrains/phpstorm-attributes": "^1.0",
45 | "phpmd/phpmd": "^2.13",
46 | "phpstan/phpstan": "^1.5",
47 | "phpunit/phpunit": "^10.5"
48 | },
49 | "minimum-stability": "dev",
50 | "prefer-stable": true,
51 | "autoload": {
52 | "psr-4": {
53 | "Framework\\Autoload\\": "src/"
54 | }
55 | },
56 | "autoload-dev": {
57 | "psr-4": {
58 | "Tests\\Autoload\\": "tests/"
59 | }
60 | },
61 | "config": {
62 | "allow-plugins": {
63 | "ergebnis/composer-normalize": true
64 | },
65 | "optimize-autoloader": true,
66 | "preferred-install": "dist",
67 | "sort-packages": true
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/Preloader.php:
--------------------------------------------------------------------------------
1 |
6 | *
7 | * For the full copyright and license information, please view the LICENSE
8 | * file that was distributed with this source code.
9 | */
10 | namespace Framework\Autoload;
11 |
12 | use InvalidArgumentException;
13 |
14 | require_once __DIR__ . '/Autoloader.php';
15 | require_once __DIR__ . '/Locator.php';
16 |
17 | /**
18 | * Class Preloader.
19 | *
20 | * @see https://www.php.net/manual/en/opcache.preloading.php
21 | *
22 | * @package autoload
23 | */
24 | class Preloader
25 | {
26 | /**
27 | * The main 'aplus' packages directory.
28 | *
29 | * @var string
30 | */
31 | protected string $packagesDir;
32 | protected bool $loadPackages = true;
33 | protected bool $loadDevPackages = false;
34 | /**
35 | * The Autoloader instance necessary to autoload required classes.
36 | *
37 | * @var Autoloader
38 | */
39 | protected Autoloader $autoloader;
40 | /**
41 | * The Locator instance used to list files.
42 | *
43 | * @var Locator
44 | */
45 | protected Locator $locator;
46 |
47 | /**
48 | * Preloader constructor.
49 | *
50 | * @param Autoloader|null $autoloader A custom Autoloader instance or null
51 | * to auto initialize a new
52 | * @param string|null $packagesDir The main 'aplus' packages directory or
53 | * null to disable packages loading
54 | */
55 | public function __construct(
56 | ?Autoloader $autoloader = null,
57 | ?string $packagesDir = __DIR__ . '/../../'
58 | ) {
59 | $this->loadPackages = $packagesDir !== null;
60 | if ($this->loadPackages) {
61 | $this->setPackagesDir($packagesDir);
62 | }
63 | $this->autoloader = $autoloader ?? new Autoloader();
64 | $this->locator = new Locator($this->autoloader);
65 | }
66 |
67 | public function getAutoloader() : Autoloader
68 | {
69 | return $this->autoloader;
70 | }
71 |
72 | public function getLocator() : Locator
73 | {
74 | return $this->locator;
75 | }
76 |
77 | public function setPackagesDir(string $packagesDir) : static
78 | {
79 | $realpath = \realpath($packagesDir);
80 | if (!$realpath || !\is_dir($packagesDir)) {
81 | throw new InvalidArgumentException('Invalid packages dir: ' . $packagesDir);
82 | }
83 | $this->packagesDir = $realpath . \DIRECTORY_SEPARATOR;
84 | return $this;
85 | }
86 |
87 | public function getPackagesDir() : string
88 | {
89 | return $this->packagesDir;
90 | }
91 |
92 | public function withPackages() : static
93 | {
94 | $this->loadPackages = true;
95 | return $this;
96 | }
97 |
98 | public function withDevPackages() : static
99 | {
100 | $this->loadDevPackages = true;
101 | return $this;
102 | }
103 |
104 | /**
105 | * @param bool $setClasses
106 | *
107 | * @return arrayTotal of = \count($includedFiles) ?> included files.
59 || # | 63 |File | 64 |Time | 65 |
|---|---|---|
| = $index + 1 ?> | 75 |= \htmlentities($file) ?> | 76 |= $data ? Debugger::roundSecondsToMilliseconds($data['end'] - $data['start']) : '' ?> | 77 |
Total of = \count($declarations) ?> declarations. 89 | 90 | = \count($classes) ?> are preloaded. 91 | 92 |
93 || # | 97 |Type | 98 |Declaration | 99 |Loaded | 100 |Time | 101 |
|---|---|---|---|---|
| = $index + 1 ?> | 113 |= $this->getDeclarationType($declaration) ?> | 114 |> 120 | = $declaration ?> 121 | | 122 |126 | | = $data ? Debugger::roundSecondsToMilliseconds($data['end'] - $data['start']) : '' ?> | 127 |
An Autoloader instance has not been set on this collector.
'; 169 | } 170 | \ob_start(); ?> 171 |No namespace directory has been set on this Autoloader instance.
'; 184 | } 185 | \ksort($namespaces); 186 | \ob_start(); ?> 187 || Namespace | 191 |Directories | 192 |
|---|---|
| = \htmlentities($namespace) ?> | 199 |= \htmlentities($directories[0]) ?> | 200 |
| = \htmlentities($directories[$i]) ?> | 204 |
No class file has been set on this Autoloader instance.
'; 218 | } 219 | \ksort($classes); 220 | \ob_start(); ?> 221 || Class | 225 |File | 226 |
|---|---|
| = \htmlentities($class) ?> | 232 |= \htmlentities($file) ?> | 233 |
Preload is not available.
'; 246 | } 247 | if ($conf && !empty($conf['directives']['opcache.preload'])) { 248 | $result = 'File: ' 249 | . \htmlentities($conf['directives']['opcache.preload']) . '
'; 250 | if (!empty($conf['directives']['opcache.preload_user'])) { 251 | $result .= 'User: ' 252 | . \htmlentities($conf['directives']['opcache.preload_user']) . '
'; 253 | } 254 | $result .= $this->renderPreloadStatistics(); 255 | return $result; 256 | } 257 | return 'Preload file has not been set.
'; 258 | } 259 | 260 | /** 261 | * @return arrayMemory: ' . $memory . '
'; 278 | $result .= 'Classes: ' . \count($classes) . '
'; 279 | } 280 | return $result; 281 | } 282 | 283 | /** 284 | * @return array