15
15
use Symfony \Component \AssetMapper \AssetMapperRepository ;
16
16
use Symfony \Component \Console \Attribute \AsCommand ;
17
17
use Symfony \Component \Console \Command \Command ;
18
+ use Symfony \Component \Console \Input \InputArgument ;
18
19
use Symfony \Component \Console \Input \InputInterface ;
20
+ use Symfony \Component \Console \Input \InputOption ;
19
21
use Symfony \Component \Console \Output \OutputInterface ;
20
22
use Symfony \Component \Console \Style \SymfonyStyle ;
21
23
@@ -40,10 +42,41 @@ public function __construct(
40
42
protected function configure (): void
41
43
{
42
44
$ this
45
+ ->addArgument ('name ' , InputArgument::OPTIONAL , 'An asset name (or a path) to search for (e.g. "app") ' )
46
+ ->addOption ('ext ' , null , InputOption::VALUE_REQUIRED , 'Filter assets by extension (e.g. "css") ' , null , ['js ' , 'css ' , 'png ' ])
43
47
->addOption ('full ' , null , null , 'Whether to show the full paths ' )
48
+ ->addOption ('vendor ' , null , InputOption::VALUE_NEGATABLE , 'Only show assets from vendor packages ' )
44
49
->setHelp (<<<'EOT'
45
- The <info>%command.name%</info> command outputs all of the assets in
46
- asset mapper for debugging purposes.
50
+ The <info>%command.name%</info> command displays information about the Asset
51
+ Mapper for debugging purposes.
52
+
53
+ To list all configured paths (with local paths and their namespace prefixes) and
54
+ all mapped assets (with their logical path and filesystem path), run:
55
+
56
+ <info>php %command.full_name%</info>
57
+
58
+ You can filter the results by providing a name to search for in the asset name
59
+ or path:
60
+
61
+ <info>php %command.full_name% bootstrap.js</info>
62
+ <info>php %command.full_name% style/</info>
63
+
64
+ To filter the assets by extension, use the <info>--ext</info> option:
65
+
66
+ <info>php %command.full_name% --ex
8000
t=css</info>
67
+
68
+ To show only assets from vendor packages, use the <info>--vendor</info> option:
69
+
70
+ <info>php %command.full_name% --vendor</info>
71
+
72
+ To exclude assets from vendor packages, use the <info>--no-vendor</info> option:
73
+
74
+ <info>php %command.full_name% --no-vendor</info>
75
+
76
+ To see the full paths, use the <info>--full</info> option:
77
+
78
+ <info>php %command.full_name% --full</info>
79
+
47
80
EOT
48
81
);
49
82
}
@@ -52,43 +85,83 @@ protected function execute(InputInterface $input, OutputInterface $output): int
52
85
{
53
86
$ io = new SymfonyStyle ($ input , $ output );
54
87
55
- $ allAssets = $ this ->assetMapper ->allAssets ();
88
+ $ name = $ input ->getArgument ('name ' );
89
+ $ extensionFilter = $ input ->getOption ('ext ' );
90
+ $ vendorFilter = $ input ->getOption ('vendor ' );
91
+
92
+ if (!$ extensionFilter ) {
93
+ $ io ->section ($ name ? 'Matched Paths ' : 'Asset Mapper Paths ' );
94
+ $ pathRows = [];
95
+ foreach ($ this ->assetMapperRepository ->allDirectories () as $ path => $ namespace ) {
96
+ $ path = $ this ->relativizePath ($ path );
97
+ if (!$ input ->getOption ('full ' )) {
98
+ $ path = $ this ->shortenPath ($ path );
99
+ }
100
+ if ($ name && !str_contains ($ path , $ name ) && !str_contains ($ namespace , $ name )) {
101
+ continue ;
102
+ }
103
+ $ pathRows [] = [$ path , $ namespace ];
104
+ }
105
+ uasort ($ pathRows , static function (array $ a , array $ b ): int {
106
+ return [(bool ) $ a [1 ], ...$ a ] <=> [(bool ) $ b [1 ], ...$ b ];
107
+ });
108
+ if ($ pathRows ) {
109
+ $ io ->table (['Path ' , 'Namespace prefix ' ], $ pathRows );
110
+ } else {
111
+ $ io ->warning ('No paths found. ' );
112
+ }
113
+ }
56
114
57
- $ pathRows = [] ;
58
- foreach ( $ this ->assetMapperRepository -> allDirectories () as $ path => $ namespace ) {
59
- $ path = $ this -> relativizePath ( $ path );
115
+ $ io -> section ( $ name ? ' Matched Assets ' : ' Mapped Assets ' ) ;
116
+ $ rows = $ this ->searchAssets ( $ name , $ extensionFilter , $ vendorFilter );
117
+ if ( $ rows ) {
60
118
if (!$ input ->getOption ('full ' )) {
61
- $ path = $ this ->shortenPath ($ path );
119
+ $ rows = array_map (fn (array $ row ): array => [
120
+ $ this ->shortenPath ($ row [0 ]),
121
+ $ this ->shortenPath ($ row [1 ]),
122
+ ], $ rows );
62
123
}
63
-
64
- $ pathRows [] = [$ path , $ namespace ];
124
+ uasort ($ rows , static function (array $ a , array $ b ): int {
125
+ return [$ a ] <=> [$ b ];
126
+ });
127
+ $ io ->table (['Logical Path ' , 'Filesystem Path ' ], $ rows );
128
+ if ($ this ->didShortenPaths ) {
129
+ $ io ->note ('To see the full paths, re-run with the --full option. ' );
130
+ }
131
+ } else {
132
+ $ io ->warning ('No assets found. ' );
65
133
}
66
- $ io ->section ('Asset Mapper Paths ' );
67
- $ io ->table (['Path ' , 'Namespace prefix ' ], $ pathRows );
68
134
135
+ return 0 ;
136
+ }
137
+
138
+ /**
139
+ * @return list<array{0:string, 1:string}>
140
+ */
141
+ private function searchAssets (?string $ name , ?string $ extension , ?bool $ vendor ): array
142
+ {
69
143
$ rows = [];
70
- foreach ($ allAssets as $ asset ) {
144
+ foreach ($ this ->assetMapper ->allAssets () as $ asset ) {
145
+ if ($ extension && $ extension !== $ asset ->publicExtension ) {
146
+ continue ;
147
+ }
148
+ if (null !== $ vendor && $ vendor !== $ asset ->isVendor ) {
149
+ continue ;
150
+ }
151
+ if ($ name && !str_contains ($ asset ->logicalPath , $ name ) && !str_contains ($ asset ->sourcePath , $ name )) {
152
+ continue ;
153
+ }
154
+
71
155
$ logicalPath = $ asset ->logicalPath ;
72
156
$ sourcePath = $ this ->relativizePath ($ asset ->sourcePath );
73
157
74
- if (!$ input ->getOption ('full ' )) {
75
- $ logicalPath = $ this ->shortenPath ($ logicalPath );
76
- $ sourcePath = $ this ->shortenPath ($ sourcePath );
77
- }
78
-
79
158
$ rows [] = [
80
159
$ logicalPath ,
81
160
$ sourcePath ,
82
161
];
83
162
}
84
- $ io ->section ('Mapped Assets ' );
85
- $ io ->table (['Logical Path ' , 'Filesystem Path ' ], $ rows );
86
-
87
- if ($ this ->didShortenPaths ) {
88
- $ io ->note ('To see the full paths, re-run with the --full option. ' );
89
- }
90
163
91
- return 0 ;
164
+ return $ rows ;
92
165
}
93
166
94
167
private function relativizePath (string $ path ): string
0 commit comments