-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileAll.php
More file actions
96 lines (84 loc) · 2.95 KB
/
CompileAll.php
File metadata and controls
96 lines (84 loc) · 2.95 KB
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
<?php
declare(strict_types=1);
namespace Firehed\API\Console;
use Composer\Autoload\ClassMapGenerator;
use Firehed\API\Config;
use Firehed\API\Dispatcher;
use Firehed\API\Interfaces\EndpointInterface;
use Firehed\Input\Interfaces\ParserInterface;
use Firehed\Input\Parsers;
use ReflectionClass;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use function array_filter;
use function array_keys;
use function array_reduce;
use function array_values;
use function get_class;
use function gmdate;
class CompileAll extends Command
{
/** @var Config */
private $config;
public function __construct(Config $config)
{
parent::__construct();
$this->config = $config;
}
protected function configure(): void
{
$this->setName('compile:all')
->setDescription('Build required static resources');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$logger = new ConsoleLogger($output);
$logger->debug('Current directory: {cwd}', ['cwd' => getcwd()]);
$logger->debug('Building classmap');
$endpoints = $this->getFilteredClasses(
$this->config->get(Config::KEY_SOURCE),
function (ReflectionClass $rc): bool {
if (!$rc->isInstantiable()) {
return false;
}
if (!$rc->implementsInterface(EndpointInterface::class)) {
return false;
}
return true;
}
);
$endpointMap = array_reduce($endpoints, function (array $carry, ReflectionClass $rc) {
$instance = $rc->newInstanceWithoutConstructor();
assert($instance instanceof EndpointInterface); // Filtered above
$carry[$instance->getMethod()][$instance->getUri()] = $rc->getName();
return $carry;
}, []);
$endpointMap['@gener'.'ated'] = gmdate('c');
file_put_contents(
Dispatcher::ENDPOINT_LIST,
sprintf("<?php\n return %s;", var_export($endpointMap, true)),
);
$output->writeln(sprintf(
'Wrote endpoint map to %s',
Dispatcher::ENDPOINT_LIST
));
return 0;
}
/**
* @param callable(ReflectionClass<object>): bool $filter
* @return ReflectionClass<object>[]
*/
private function getFilteredClasses(string $directory, callable $filter): array
{
/** @var array<class-string, string> */
$cm = ClassMapGenerator::createMap($directory);
$classes = array_keys($cm);
$rcs = array_map(fn ($fqcn) => new ReflectionClass($fqcn), $classes);
$result = array_filter($rcs, $filter);
// Compact the result set
return array_values($result);
}
}