Skip to content

Commit 5cf723b

Browse files
committed
pref: mise en cache du localisateur de fichiers
1 parent efac717 commit 5cf723b

4 files changed

Lines changed: 256 additions & 2 deletions

File tree

Locator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @credit <a href="https://codeigniter.com">CodeIgniter4 - CodeIgniter\Autoloader\FileLocator</a>
1919
*/
20-
class Locator
20+
class Locator implements LocatorInterface
2121
{
2222
/**
2323
* Autoloader a utiliser.

LocatorCached.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Blitz PHP framework.
5+
*
6+
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace BlitzPHP\Autoloader;
13+
14+
use Psr\SimpleCache\CacheInterface;
15+
16+
/**
17+
* Localisateur de fichiers avec cache
18+
*
19+
* @credit <a href="https://codeigniter.com">CodeIgniter4 - CodeIgniter\Autoloader\FileLocatorCached</a>
20+
*/
21+
final class LocatorCached implements LocatorInterface
22+
{
23+
/**
24+
* Donnees mise en cach
25+
*
26+
* [method => data]
27+
* E.g.,
28+
* [
29+
* 'search' => [$path => $foundPaths],
30+
* ]
31+
*/
32+
private array $cache = [];
33+
34+
/**
35+
* Le cache est-il mis à jour ?
36+
*/
37+
private bool $cacheUpdated = false;
38+
39+
private string $cacheKey = 'FileLocatorCache';
40+
41+
/**
42+
* Constructor
43+
*/
44+
public function __construct(private Locator $locator, private CacheInterface $cacheHandler)
45+
{
46+
$this->loadCache();
47+
}
48+
49+
private function loadCache(): void
50+
{
51+
$data = $this->cacheHandler->get($this->cacheKey);
52+
53+
if (is_array($data)) {
54+
$this->cache = $data;
55+
}
56+
}
57+
58+
public function __destruct()
59+
{
60+
$this->saveCache();
61+
}
62+
63+
private function saveCache(): void
64+
{
65+
if ($this->cacheUpdated) {
66+
$this->cacheHandler->set($this->cacheKey, $this->cache, 3600 * 24);
67+
}
68+
}
69+
70+
/**
71+
* Supprime les donnees de du cache
72+
*/
73+
public function deleteCache(): void
74+
{
75+
$this->cacheHandler->delete($this->cacheKey);
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
public function findQualifiedNameFromPath(string $path)
82+
{
83+
if (isset($this->cache['findQualifiedNameFromPath'][$path])) {
84+
return $this->cache['findQualifiedNameFromPath'][$path];
85+
}
86+
87+
$classname = $this->locator->findQualifiedNameFromPath($path);
88+
89+
$this->cache['findQualifiedNameFromPath'][$path] = $classname;
90+
$this->cacheUpdated = true;
91+
92+
return $classname;
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
public function getClassname(string $file): string
99+
{
100+
if (isset($this->cache['getClassname'][$file])) {
101+
return $this->cache['getClassname'][$file];
102+
}
103+
104+
$classname = $this->locator->getClassname($file);
105+
106+
$this->cache['getClassname'][$file] = $classname;
107+
$this->cacheUpdated = true;
108+
109+
return $classname;
110+
}
111+
112+
/**
113+
* {@inheritDoc}
114+
*/
115+
public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array
116+
{
117+
if (isset($this->cache['search'][$path][$ext][$prioritizeApp])) {
118+
return $this->cache['search'][$path][$ext][$prioritizeApp];
119+
}
120+
121+
$foundPaths = $this->locator->search($path, $ext, $prioritizeApp);
122+
123+
$this->cache['search'][$path][$ext][$prioritizeApp] = $foundPaths;
124+
$this->cacheUpdated = true;
125+
126+
return $foundPaths;
127+
}
128+
129+
/**
130+
* {@inheritDoc}
131+
*/
132+
public function listFiles(string $path): array
133+
{
134+
if (isset($this->cache['listFiles'][$path])) {
135+
return $this->cache['listFiles'][$path];
136+
}
137+
138+
$files = $this->locator->listFiles($path);
139+
140+
$this->cache['listFiles'][$path] = $files;
141+
$this->cacheUpdated = true;
142+
143+
return $files;
144+
}
145+
146+
/**
147+
* {@inheritDoc}
148+
*/
149+
public function listNamespaceFiles(string $prefix, string $path): array
150+
{
151+
if (isset($this->cache['listNamespaceFiles'][$prefix][$path])) {
152+
return $this->cache['listNamespaceFiles'][$prefix][$path];
153+
}
154+
155+
$files = $this->locator->listNamespaceFiles($prefix, $path);
156+
157+
$this->cache['listNamespaceFiles'][$prefix][$path] = $files;
158+
$this->cacheUpdated = true;
159+
160+
return $files;
161+
}
162+
163+
/**
164+
* {@inheritDoc}
165+
*/
166+
public function locateFile(string $file, ?string $folder = null, string $ext = 'php'): false|string
167+
{
168+
if (isset($this->cache['locateFile'][$file][$folder][$ext])) {
169+
return $this->cache['locateFile'][$file][$folder][$ext];
170+
}
171+
172+
$files = $this->locator->locateFile($file, $folder, $ext);
173+
174+
$this->cache['locateFile'][$file][$folder][$ext] = $files;
175+
$this->cacheUpdated = true;
176+
177+
return $files;
178+
}
179+
}

LocatorInterface.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Blitz PHP framework.
5+
*
6+
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace BlitzPHP\Autoloader;
13+
14+
/**
15+
* Permet de charger des fichiers non-classes avec un espace de noms.
16+
* Fonctionne avec les helpers, les vues, etc.
17+
*/
18+
interface LocatorInterface
19+
{
20+
/**
21+
* Tente de localiser un fichier en examinant le nom d'un espace de noms
22+
* et en parcourant les fichiers d'espace de noms PSR-4 que nous connaissons.
23+
*
24+
* @param string $file Le fichier d'espace de noms à localiser
25+
* @param string|null $folder Le dossier dans l'espace de noms où nous devons rechercher le fichier.
26+
* @param string $ext L'extension de fichier que le fichier doit avoir.
27+
*
28+
* @return false|string Le chemin d'accès au fichier, ou false s'il n'est pas trouvé.
29+
*/
30+
public function locateFile(string $file, ?string $folder = null, string $ext = 'php');
31+
32+
/**
33+
* Examine une fichier et retourne le FQCN.
34+
*/
35+
public function getClassname(string $file): string;
36+
37+
/**
38+
* Recherche dans tous les espaces de noms définis à la recherche d'un fichier.
39+
* Renvoie un tableau de tous les emplacements trouvés pour le fichier défini.
40+
*
41+
* Exemple:
42+
*
43+
* $locator->search('Config/Routes.php');
44+
* // Assuming PSR4 namespaces include foo and bar, might return:
45+
* [
46+
* 'app/Modules/foo/Config/Routes.php',
47+
* 'app/Modules/bar/Config/Routes.php',
48+
* ]
49+
*/
50+
public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array;
51+
52+
/**
53+
* Recherchez le nom qualifié d'un fichier en fonction de l'espace de noms du premier chemin d'espace de noms correspondant.
54+
*
55+
* @return false|string Le nom qualifié ou false si le chemin n'est pas trouvé
56+
*/
57+
public function findQualifiedNameFromPath(string $path);
58+
59+
/**
60+
* Scane les namespace definis, retourne une liste de tous les fichiers
61+
* contenant la sous partie specifiee par $path.
62+
*
63+
* @return string[] Liste des fichiers du chemins
64+
*/
65+
public function listFiles(string $path): array;
66+
67+
/**
68+
* Analyse l'espace de noms fourni, renvoyant une liste de tous les fichiers
69+
* contenus dans le sous-chemin spécifié par $path.
70+
*
71+
* @return string[] Liste des chemins des fichiers
72+
*/
73+
public function listNamespaceFiles(string $prefix, string $path): array;
74+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=8.0"
13+
"php": "^8.1",
14+
"psr/simple-cache": "^2.0"
1415
},
1516
"autoload": {
1617
"psr-4": {

0 commit comments

Comments
 (0)