Skip to content

Commit 971416f

Browse files
authored
Create PackageAutoloadGenerator.php
1 parent 729119c commit 971416f

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed
+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
namespace Webfan\Autoload;
4+
use Frdlweb\Contract\Autoload\LoaderInterface;
5+
use Frdlweb\Contract\Autoload\ClassLoaderInterface;
6+
use Frdlweb\Contract\Autoload\Psr4GeneratorInterface;
7+
use Frdlweb\Contract\Autoload\ClassmapGeneratorInterface;
8+
use Frdlweb\Contract\Autoload\GeneratorInterface;
9+
use Frdlweb\Contract\Autoload\ResolverInterface;
10+
use Frdlweb\Contract\Autoload\ConditionalResolverInterface;
11+
use Frdlweb\Contract\Autoload\ContextInterface;
12+
use Nette;
13+
use SplFileInfo;
14+
use Webfan\Traits\WithContextDirectories as DirectoriesTrait;
15+
use Webfan\Traits\WithTimeout;
16+
17+
18+
/**
19+
use Frdlweb\Contract\Autoload;
20+
21+
interface ClassmapGeneratorInterface {
22+
public function addDirectory(string $dir);
23+
}
24+
25+
use Frdlweb\Contract\Autoload\GeneratorInterface;
26+
public function withDirectory($dir);
27+
public function withAlias(string $alias, string $rewrite);
28+
public function withClassmap(array $classMap = null);
29+
public function withNamespace($prefix, $server, $prepend = false);
30+
31+
\Frdlweb\Contract\Autoload\ClassmapGeneratorInterface
32+
public function addDirectory(string $dir);
33+
\Frdlweb\Contract\Autoload\Psr4GeneratorInterface
34+
public function $this->Psr4Generator->addNamespace($prefix, $resourceOrLocation, $prepend = false);
35+
*/
36+
37+
class PackageAutoloadGenerator implements LoaderInterface
38+
{
39+
use Nette\SmartObject;
40+
41+
protected $packageDirectories = [];
42+
protected $ClassmapGenerator = null;
43+
protected $Psr4Generator = null;
44+
protected $RemoteAutoloader = null;
45+
protected $allowRedundancy = false;
46+
protected $_toRegister = [];
47+
48+
49+
public function __construct( $allowRedundancy = false, ClassmapGeneratorInterface $ClassmapGenerator = null,
50+
Psr4GeneratorInterface $Psr4Generator = null,
51+
LoaderInterface $RemoteAutoloader = null
52+
){
53+
$this->ClassmapGenerator = (null !== $ClassmapGenerator) ? $ClassmapGenerator : new \Webfan\Autoload\CodebaseLoader;
54+
$this->Psr4Generator = (null !== $Psr4Generator) ? $Psr4Generator : new \Webfan\Autoload\LocalPsr4Autoloader;
55+
$this->RemoteAutoloader = (null !== $RemoteAutoloader) ? $RemoteAutoloader : null; // @Lacks Interface! new \Webfan\Autoload\RemoteFallbackLoader;
56+
$this->allowRedundancy=$allowRedundancy;
57+
}
58+
59+
public function withRedundancy(bool $allowRedundancy = false ){
60+
$this->allowRedundancy=$allowRedundancy;
61+
return $this;
62+
}
63+
64+
public function addDirectory(string $dir){
65+
return $this->_load($dir);
66+
}
67+
public function load(string $dir){
68+
return $this->_load($dir);
69+
}
70+
public function withDirectory($dir){
71+
return $this->_load($dir);
72+
}
73+
74+
protected function _withDirectoryClassmap($dir){
75+
$this->ClassmapGenerator->addDirectory($dir);
76+
return $this;
77+
}
78+
public function loadClassmaps($classMaps){
79+
if (!is_array($classMaps)) {
80+
$classMaps =[$classMaps];
81+
}
82+
foreach($classMaps as $path){
83+
if(file_exists($path) && is_file($path)){
84+
$path = dirname($path);
85+
}
86+
$this->_withDirectoryClassmap($path);
87+
}
88+
return $this;
89+
}
90+
protected function _getComposerFile($dir)
91+
{
92+
if(!isset($this->packageDirectories[$dir])){
93+
$this->packageDirectories[$dir]=[];
94+
}
95+
try{
96+
$this->packageDirectories[$dir]['json'] = json_decode(file_get_contents($dir."/composer.json"), 1);
97+
}catch(\Exception $e){
98+
trigger_error(sprintf('% %', $e->getMessage() , __METHOD__), \E_USER_WARNING);
99+
return [];
100+
}
101+
102+
103+
104+
return $this->packageDirectories[$dir]['json'];
105+
}
106+
107+
108+
109+
110+
111+
protected function _load($dir)
112+
{
113+
$this->dir = $dir;
114+
$composer = $this->_getComposerFile($dir);
115+
116+
if(isset($composer["autoload"]["classmap"])){
117+
$this->loadClassmaps($composer["autoload"]["classmap"]);
118+
}
119+
120+
if(isset($composer["autoload"]["psr-4"])){
121+
$this->loadPSR4($composer['autoload']['psr-4']);
122+
}
123+
if(isset($composer["autoload"]["psr-0"])){
124+
$this->loadPSR0($composer['autoload']['psr-0']);
125+
}
126+
if(isset($composer["autoload"]["files"])){
127+
$this->loadFiles($composer["autoload"]["files"]);
128+
}
129+
}
130+
131+
public function loadFiles($files){
132+
foreach($files as $file){
133+
$fullpath = //$this->dir."/".
134+
$file;
135+
if(file_exists($fullpath)){
136+
include $fullpath;
137+
}
138+
}
139+
}
140+
141+
public function loadPSR4($namespaces)
142+
{
143+
$this->loadPSR($namespaces, true);
144+
}
145+
146+
public function loadPSR0($namespaces)
147+
{
148+
$this->loadPSR($namespaces, false);
149+
}
150+
151+
public function loadPSR($namespaces, $psr4, $prepend = false)
152+
{
153+
// $dir = $this->dir;
154+
// Foreach namespace specified in the composer, load the given classes
155+
foreach ($namespaces as $namespace => $classpaths) {
156+
if (!is_array($classpaths)) {
157+
$classpaths = [$classpaths];
158+
}
159+
if ($psr4) {
160+
foreach ($classpaths as $_classpath) {
161+
$this->Psr4Generator->addNamespace($namespace, $_classpath, $prepend);
162+
}
163+
164+
}
165+
166+
167+
// spl_autoload_register(
168+
$this->_toRegister[] =( function ($classname) use ($namespace, $classpaths, $dir, $psr4) {
169+
// Check if the namespace matches the class we are looking for
170+
if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
171+
// Remove the namespace from the file path since it's psr4
172+
if ($psr4) {
173+
$classname = str_replace($namespace, "", $classname);
174+
}
175+
$filename = preg_replace("#\\\\#", "/", $classname).".php";
176+
foreach ($classpaths as $classpath) {
177+
$fullpath = $classpath."/$filename";
178+
if (file_exists($fullpath)) {
179+
include $fullpath;
180+
}
181+
}
182+
}
183+
}
184+
)
185+
;
186+
}
187+
}
188+
189+
public function register(bool $prepend = false){
190+
$this->ClassmapGenerator->register($prepend);
191+
$this->Psr4Generator->register($prepend);
192+
if(null !== $this->RemoteAutoloader){
193+
$this->RemoteAutoloader->register($prepend);
194+
}
195+
196+
}
197+
198+
}

0 commit comments

Comments
 (0)