-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
AutoloadGenerator.php
110 lines (92 loc) · 3.08 KB
/
AutoloadGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @see https://github.com/laminas/laminas-di for the canonical source repository
* @copyright https://github.com/laminas/laminas-di/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-di/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace Laminas\Di\CodeGenerator;
use Laminas\Di\Exception\GenerateCodeException;
use SplFileObject;
use Throwable;
use function array_keys;
use function array_map;
use function assert;
use function file_get_contents;
use function implode;
use function is_string;
use function sprintf;
use function str_repeat;
use function strtr;
use function var_export;
class AutoloadGenerator
{
use GeneratorTrait;
private const CLASS_TEMPLATE = __DIR__ . '/../../templates/autoloader-class.template';
private const FILE_TEMPLATE = __DIR__ . '/../../templates/autoloader-file.template';
/** @var string */
private $namespace;
public function __construct(string $namespace)
{
$this->namespace = $namespace;
}
private function writeFile(string $filename, string $code): void
{
try {
$file = new SplFileObject($filename, 'w');
$file->fwrite($code);
} catch (Throwable $e) {
throw new GenerateCodeException(sprintf('Failed to write output file "%s"', $filename), 0, $e);
}
}
private function buildFromTemplate(string $templateFile, string $outputFile, array $replacements): void
{
$template = file_get_contents($templateFile);
assert(is_string($template));
$this->writeFile(
sprintf('%s/%s', $this->outputDirectory, $outputFile),
strtr(
$template,
$replacements
)
);
}
private function generateClassmapCode(array &$classmap): string
{
$lines = array_map(
function (string $class, string $file): string {
return sprintf(
'%s => %s,',
var_export($class, true),
var_export($file, true)
);
},
array_keys($classmap),
$classmap
);
$indentation = sprintf("\n%s", str_repeat(' ', 8));
return implode($indentation, $lines);
}
private function generateAutoloaderClass(array &$classmap): void
{
$this->buildFromTemplate(self::CLASS_TEMPLATE, 'Autoloader.php', [
'%namespace%' => $this->namespace ? sprintf("namespace %s;\n", $this->namespace) : '',
'%classmap%' => $this->generateClassmapCode($classmap),
]);
}
private function generateAutoloadFile(): void
{
$this->buildFromTemplate(self::FILE_TEMPLATE, 'autoload.php', [
'%namespace%' => $this->namespace ? sprintf("namespace %s;\n", $this->namespace) : '',
]);
}
/**
* @param string[] $classmap
*/
public function generate(array &$classmap): void
{
$this->ensureOutputDirectory();
$this->generateAutoloaderClass($classmap);
$this->generateAutoloadFile();
}
}