|
7 | 7 | use Composer\IO\IOInterface; |
8 | 8 | use Composer\Plugin\PluginInterface; |
9 | 9 | use Composer\Script\Event; |
10 | | -use Composer\Util\Platform; |
11 | | -use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore; |
12 | | -use olvlvl\ComposerAttributeCollector\Datastore\RuntimeDatastore; |
13 | | -use olvlvl\ComposerAttributeCollector\Filter\ContentFilter; |
14 | | -use olvlvl\ComposerAttributeCollector\Filter\ClassFilter; |
15 | | -use olvlvl\ComposerAttributeCollector\Logger\ComposerLogger; |
| 10 | +use Symfony\Component\Process\Process; |
16 | 11 |
|
17 | 12 | use function file_exists; |
18 | 13 | use function file_put_contents; |
19 | 14 | use function microtime; |
20 | | -use function sprintf; |
21 | 15 |
|
22 | 16 | use const DIRECTORY_SEPARATOR; |
23 | 17 |
|
@@ -82,94 +76,22 @@ public function uninstall(Composer $composer, IOInterface $io): void |
82 | 76 | public static function onPostAutoloadDump(Event $event): void |
83 | 77 | { |
84 | 78 | $composer = $event->getComposer(); |
85 | | - $config = Config::from($composer); |
86 | 79 | $io = $event->getIO(); |
| 80 | + $config = Config::from($composer, isDebug: $io->isDebug()); |
87 | 81 |
|
88 | 82 | require_once $config->vendorDir . "/autoload.php"; |
89 | 83 |
|
90 | | - $start = microtime(true); |
91 | 84 | $io->write('<info>Generating attributes file</info>'); |
92 | | - $logger = new ComposerLogger($io); |
93 | | - self::dump($config, $logger); |
94 | | - $elapsed = self::renderElapsedTime($start); |
95 | | - $io->write("<info>Generated attributes file in $elapsed</info>"); |
96 | | - } |
97 | | - |
98 | | - public static function dump(Config $config, Logger $log): void |
99 | | - { |
100 | | - // |
101 | | - // Scan the included paths |
102 | | - // |
103 | | - $start = microtime(true); |
104 | | - $datastore = self::buildDefaultDatastore($config, $log); |
105 | | - $classMapGenerator = new MemoizeClassMapGenerator($datastore, $log); |
106 | | - foreach ($config->include as $include) { |
107 | | - $classMapGenerator->scanPaths($include, $config->excludeRegExp); |
108 | | - } |
109 | | - $classMap = $classMapGenerator->getMap(); |
110 | | - $elapsed = self::renderElapsedTime($start); |
111 | | - $log->debug("Generating attributes file: scanned paths in $elapsed"); |
112 | | - |
113 | | - // |
114 | | - // Filter the class map |
115 | | - // |
116 | | - $start = microtime(true); |
117 | | - $classMapFilter = new MemoizeClassMapFilter($datastore, $log); |
118 | | - $filter = self::buildFileFilter(); |
119 | | - $classMap = $classMapFilter->filter( |
120 | | - $classMap, |
121 | | - fn (string $class, string $filepath): bool => $filter->filter($filepath, $class, $log) |
122 | | - ); |
123 | | - $elapsed = self::renderElapsedTime($start); |
124 | | - $log->debug("Generating attributes file: filtered class map in $elapsed"); |
125 | | - |
126 | | - // |
127 | | - // Collect attributes |
128 | | - // |
129 | 85 | $start = microtime(true); |
130 | | - $attributeCollector = new MemoizeAttributeCollector(new ClassAttributeCollector($log), $datastore, $log); |
131 | | - $collection = $attributeCollector->collectAttributes($classMap); |
132 | | - $elapsed = self::renderElapsedTime($start); |
133 | | - $log->debug("Generating attributes file: collected attributes in $elapsed"); |
134 | | - |
135 | | - // |
136 | | - // Render attributes |
137 | | - // |
138 | | - $start = microtime(true); |
139 | | - $code = self::render($collection); |
140 | | - file_put_contents($config->attributesFile, $code); |
141 | | - $elapsed = self::renderElapsedTime($start); |
142 | | - $log->debug("Generating attributes file: rendered code in $elapsed"); |
143 | | - } |
144 | | - |
145 | | - private static function buildDefaultDatastore(Config $config, Logger $log): Datastore |
146 | | - { |
147 | | - if (!$config->useCache) { |
148 | | - return new RuntimeDatastore(); |
149 | | - } |
150 | | - |
151 | | - $basePath = Platform::getCwd(); |
152 | | - |
153 | | - assert($basePath !== ''); |
154 | | - |
155 | | - return new FileDatastore($basePath . DIRECTORY_SEPARATOR . self::CACHE_DIR, $log); |
156 | | - } |
157 | | - |
158 | | - private static function renderElapsedTime(float $start): string |
159 | | - { |
160 | | - return sprintf("%.03f ms", (microtime(true) - $start) * 1000); |
161 | | - } |
162 | | - |
163 | | - private static function buildFileFilter(): Filter |
164 | | - { |
165 | | - return new Filter\Chain([ |
166 | | - new ContentFilter(), |
167 | | - new ClassFilter() |
168 | | - ]); |
| 86 | + self::dump($config); |
| 87 | + $elapsed = ElapsedTime::render($start); |
| 88 | + $io->write("<info>Generated attributes file in $elapsed</info>"); |
169 | 89 | } |
170 | 90 |
|
171 | | - private static function render(TransientCollection $collector): string |
| 91 | + public static function dump(Config $config): void |
172 | 92 | { |
173 | | - return TransientCollectionRenderer::render($collector); |
| 93 | + $cmd = __DIR__ . '/../collector.php'; |
| 94 | + $process = new Process([ $cmd, '--', serialize($config) ]); |
| 95 | + $process->mustRun(fn (string $type, string $line) => print($line)); |
174 | 96 | } |
175 | 97 | } |
0 commit comments