11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Qossmic \TwigDocBundle \DependencyInjection \Compiler ;
46
57use Qossmic \TwigDocBundle \Component \ComponentCategory ;
1113use Symfony \Component \Finder \Finder ;
1214use Symfony \Component \Finder \SplFileInfo ;
1315
14- class TwigDocCollectDocsPass implements CompilerPassInterface
16+ readonly class TwigDocCollectDocsPass implements CompilerPassInterface
1517{
16- public function __construct (private readonly ParserInterface $ parser )
18+ public function __construct (private ParserInterface $ parser )
1719 {
1820 }
1921
@@ -22,6 +24,7 @@ public function process(ContainerBuilder $container): void
2224 if (!$ container ->hasExtension ('twig_doc ' )) {
2325 return ;
2426 }
27+
2528 $ config = $ container ->getParameter ('twig_doc.config ' );
2629 $ directories = $ this ->resolveDirectories ($ container , $ config ['directories ' ]);
2730 $ container ->getParameterBag ()->remove ('twig_doc.config ' );
@@ -51,9 +54,10 @@ public function process(ContainerBuilder $container): void
5154 $ filename = $ file ->getFilename ();
5255 $ componentName = substr ($ filename , 0 , strpos ($ filename , '. ' ));
5356
54- if (array_filter ($ componentConfig , fn (array $ data ) => $ data ['name ' ] === $ componentName )) {
55- throw new InvalidConfigException (sprintf ('component "%s" is configured twice, please configure either directly in the template or the general bundle configuration ' , $ componentName ));
57+ if (array_filter ($ componentConfig , static fn (array $ data ) => $ data ['name ' ] === $ componentName )) {
58+ throw new InvalidConfigException (sprintf ('Component "%s" is configured twice, please configure either directly in the template or the general bundle configuration ' , $ componentName ));
5659 }
60+
5761 $ itemConfig = [
5862 'name ' => $ componentName ,
5963 'path ' => str_replace ($ projectDir .'/ ' , '' , $ file ->getRealPath ()),
@@ -69,7 +73,6 @@ public function process(ContainerBuilder $container): void
6973 private function parseDoc (SplFileInfo $ file , string $ docIdentifier ): ?array
7074 {
7175 $ content = $ file ->getContents ();
72-
7376 $ pattern = sprintf ("/\{#%s\s(.*)%s#}/s " , $ docIdentifier , $ docIdentifier );
7477
7578 preg_match ($ pattern , $ content , $ matches );
@@ -84,10 +87,10 @@ private function parseDoc(SplFileInfo $file, string $docIdentifier): ?array
8487 private function enrichComponentsConfig (ContainerBuilder $ container , array $ directories , array $ components ): array
8588 {
8689 foreach ($ components as &$ component ) {
87- if (!isset ($ component ['path ' ])) {
88- $ component ['path ' ] = str_replace ($ container ->getParameter ('kernel.project_dir ' ).'/ ' , '' , $ this -> getTemplatePath ( $ component [ ' name ' ], $ directories ) );
90+ if (!isset ($ component ['path ' ]) && $ templatePath = $ this -> getTemplatePath ( $ component [ ' name ' ], $ directories ) ) {
91+ $ component ['path ' ] = str_replace ($ container ->getParameter ('kernel.project_dir ' ).'/ ' , '' , $ templatePath );
8992 }
90- if (!isset ($ component ['renderPath ' ])) {
93+ if (!isset ($ component ['renderPath ' ]) && isset ( $ component [ ' path ' ]) ) {
9194 $ component ['renderPath ' ] = str_replace ($ container ->getParameter ('twig.default_path ' ).'/ ' , '' , $ component ['path ' ]);
9295 }
9396 }
@@ -98,8 +101,7 @@ private function enrichComponentsConfig(ContainerBuilder $container, array $dire
98101 private function resolveDirectories (ContainerBuilder $ container , array $ directories ): array
99102 {
100103 $ directories [] = $ container ->getParameter ('twig.default_path ' ).'/components ' ;
101-
102- $ directories = array_map (fn (string $ dir ) => $ container ->getParameterBag ()->resolveValue ($ dir ), $ directories );
104+ $ directories = array_map (static fn (string $ dir ) => $ container ->getParameterBag ()->resolveValue ($ dir ), $ directories );
103105
104106 foreach ($ directories as $ idx => $ dir ) {
105107 if (!is_dir ($ dir )) {
@@ -113,9 +115,7 @@ private function resolveDirectories(ContainerBuilder $container, array $director
113115 private function getTemplatePath (string $ name , array $ directories ): ?string
114116 {
115117 $ template = sprintf ('%s.html.twig ' , $ name );
116-
117118 $ finder = new Finder ();
118-
119119 $ files = $ finder ->in ($ directories )->files ()->filter (fn (SplFileInfo $ file ) => $ file ->getFilename () === $ template );
120120
121121 if ($ files ->count () > 1 ) {
@@ -128,6 +128,6 @@ private function getTemplatePath(string $name, array $directories): ?string
128128
129129 $ files ->getIterator ()->rewind ();
130130
131- return $ files ->getIterator ()->current ();
131+ return $ files ->getIterator ()->current ()?->__toString() ;
132132 }
133133}
0 commit comments