13
13
14
14
use Symfony \Component \AssetMapper \AssetMapper ;
15
15
use Symfony \Component \AssetMapper \AssetMapperInterface ;
16
+ use Symfony \Component \AssetMapper \CompiledAssetMapperConfigReader ;
16
17
use Symfony \Component \AssetMapper \Event \PreAssetsCompileEvent ;
17
18
use Symfony \Component \AssetMapper \ImportMap \ImportMapManager ;
19
+ use Symfony \Component \AssetMapper \Path \PublicAssetsFilesystemInterface ;
18
20
use Symfony \Component \AssetMapper \Path \PublicAssetsPathResolverInterface ;
19
21
use Symfony \Component \Console \Attribute \AsCommand ;
20
22
use Symfony \Component \Console \Command \Command ;
36
38
final class AssetMapperCompileCommand extends Command
37
39
{
38
40
public function __construct (
39
- private readonly PublicAssetsPathResolverInterface $ publicAssetsPathResolver ,
41
+ private readonly CompiledAssetMapperConfigReader $ compiledConfigReader ,
40
42
private readonly AssetMapperInterface $ assetMapper ,
41
43
private readonly ImportMapManager $ importMapManager ,
42
- private readonly Filesystem $ filesystem ,
44
+ private readonly PublicAssetsFilesystemInterface $ assetsFilesystem ,
43
45
private readonly string $ projectDir ,
44
- private readonly string $ publicDirName ,
45
46
private readonly bool $ isDebug ,
46
47
private readonly ?EventDispatcherInterface $ eventDispatcher = null ,
47
48
) {
@@ -51,7 +52,6 @@ public function __construct(
51
52
protected function configure (): void
52
53
{
53
54
$ this
54
- ->addOption ('clean ' , null , null , 'Whether to clean the public directory before compiling assets ' )
55
55
->setHelp (<<<'EOT'
56
56
The <info>%command.name%</info> command compiles and dumps all the assets in
57
57
the asset mapper into the final public directory (usually <comment>public/assets</comment>).
@@ -64,61 +64,36 @@ protected function configure(): void
64
64
protected function execute (InputInterface $ input , OutputInterface $ output ): int
65
65
{
66
66
$ io = new SymfonyStyle ($ input , $ output );
67
- $ publicDir = $ this ->projectDir .'/ ' .$ this ->publicDirName ;
68
- if (!is_dir ($ publicDir )) {
69
- throw new InvalidArgumentException (sprintf ('The public directory "%s" does not exist. ' , $ publicDir ));
70
- }
71
-
72
- $ outputDir = $ this ->publicAssetsPathResolver ->getPublicFilesystemPath ();
73
- if ($ input ->getOption ('clean ' )) {
74
- $ io ->comment (sprintf ('Cleaning <info>%s</info> ' , $ outputDir ));
75
- $ this ->filesystem ->remove ($ outputDir );
76
- $ this ->filesystem ->mkdir ($ outputDir );
77
- }
78
67
79
- // set up the file paths
80
- $ files = [];
81
- $ manifestPath = $ outputDir .'/ ' .AssetMapper::MANIFEST_FILE_NAME ;
82
- $ files [] = $ manifestPath ;
68
+ $ this ->eventDispatcher ?->dispatch(new PreAssetsCompileEvent ($ output ));
83
69
84
- $ importMapPath = $ outputDir . ' / ' .ImportMapManager:: IMPORT_MAP_CACHE_FILENAME ;
85
- $ files [] = $ importMapPath ;
86
-
87
- $ entrypointFilePaths = [];
70
+ // remove existing config files
71
+ $ this -> compiledConfigReader -> removeConfig (AssetMapper:: MANIFEST_FILE_NAME ) ;
72
+ $ this -> compiledConfigReader -> removeConfig (ImportMapManager:: IMPORT_MAP_CACHE_FILENAME );
73
+ $ entrypointFiles = [];
88
74
foreach ($ this ->importMapManager ->getEntrypointNames () as $ entrypointName ) {
89
- $ dumpedEntrypointPath = $ outputDir . ' / ' . sprintf (ImportMapManager::ENTRYPOINT_CACHE_FILENAME_PATTERN , $ entrypointName );
90
- $ files [] = $ dumpedEntrypointPath ;
91
- $ entrypointFilePaths [$ entrypointName ] = $ dumpedEntrypointPath ;
75
+ $ path = sprintf (ImportMapManager::ENTRYPOINT_CACHE_FILENAME_PATTERN , $ entrypointName );
76
+ $ this -> compiledConfigReader -> removeConfig ( $ path ) ;
77
+ $ entrypointFiles [$ entrypointName ] = $ path ;
92
78
}
93
79
94
- // remove existing files
95
- foreach ($ files as $ file ) {
96
- if (is_file ($ file )) {
97
- $ this ->filesystem ->remove ($ file );
98
- }
99
- }
100
-
101
- $ this ->eventDispatcher ?->dispatch(new PreAssetsCompileEvent ($ outputDir , $ output ));
102
-
103
- // dump new files
104
- $ manifest = $ this ->createManifestAndWriteFiles ($ io , $ publicDir );
105
- $ this ->filesystem ->dumpFile ($ manifestPath , json_encode ($ manifest , \JSON_PRETTY_PRINT ));
80
+ $ manifest = $ this ->createManifestAndWriteFiles ($ io );
81
+ $ manifestPath = $ this ->compiledConfigReader ->saveConfig (AssetMapper::MANIFEST_FILE_NAME , $ manifest );
106
82
$ io ->comment (sprintf ('Manifest written to <info>%s</info> ' , $ this ->shortenPath ($ manifestPath )));
107
83
108
- $ this ->filesystem -> dumpFile ( $ importMapPath , json_encode ( $ this ->importMapManager ->getRawImportMapData (), \ JSON_THROW_ON_ERROR | \ JSON_PRETTY_PRINT | \ JSON_UNESCAPED_SLASHES | \ JSON_HEX_TAG ));
84
+ $ importMapPath = $ this ->compiledConfigReader -> saveConfig (ImportMapManager:: IMPORT_MAP_CACHE_FILENAME , $ this ->importMapManager ->getRawImportMapData ());
109
85
$ io ->comment (sprintf ('Import map data written to <info>%s</info>. ' , $ this ->shortenPath ($ importMapPath )));
110
86
111
- $ entrypointNames = $ this ->importMapManager ->getEntrypointNames ();
112
- foreach ($ entrypointFilePaths as $ entrypointName => $ path ) {
113
- $ this ->filesystem ->dumpFile ($ path , json_encode ($ this ->importMapManager ->getEntrypointMetadata ($ entrypointName ), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG ));
87
+ foreach ($ entrypointFiles as $ entrypointName => $ path ) {
88
+ $ this ->compiledConfigReader ->saveConfig ($ path , $ this ->importMapManager ->getEntrypointMetadata ($ entrypointName ));
114
89
}
115
- $ styledEntrypointNames = array_map (fn (string $ entrypointName ) => sprintf ('<info>%s</> ' , $ entrypointName ), $ entrypointNames );
116
- $ io ->comment (sprintf ('Entrypoint metadata written for <comment>%d</> entrypoints (%s). ' , \count ($ entrypointNames ), implode (', ' , $ styledEntrypointNames )));
90
+ $ styledEntrypointNames = array_map (fn (string $ entrypointName ) => sprintf ('<info>%s</> ' , $ entrypointName ), array_keys ( $ entrypointFiles ) );
91
+ $ io ->comment (sprintf ('Entrypoint metadata written for <comment>%d</> entrypoints (%s). ' , \count ($ entrypointFiles ), implode (', ' , $ styledEntrypointNames )));
117
92
118
93
if ($ this ->isDebug ) {
119
94
$ io ->warning (sprintf (
120
- 'You are compiling assets in development. Symfony will not serve any changed assets until you delete the "%s" directory. ' ,
121
- $ this ->shortenPath ($ outputDir )
95
+ 'You are compiling assets in development. Symfony will not serve any changed assets until you delete the files in the "%s" directory. ' ,
96
+ $ this ->shortenPath (dirname ( $ manifestPath ) )
122
97
));
123
98
}
124
99
@@ -130,21 +105,14 @@ private function shortenPath(string $path): string
130
105
return str_replace ($ this ->projectDir .'/ ' , '' , $ path );
131
106
}
132
107
133
- private function createManifestAndWriteFiles (SymfonyStyle $ io, string $ publicDir ): array
108
+ private function createManifestAndWriteFiles (SymfonyStyle $ io ): array
134
109
{
135
110
$ allAssets = $ this ->assetMapper ->allAssets ();
136
111
137
- $ io ->comment (sprintf ('Compiling assets to <info>%s%s </info> ' , $ publicDir , $ this ->publicAssetsPathResolver -> resolvePublicPath ( '' )));
112
+ $ io ->comment (sprintf ('Compiling and writing asset files to <info>%s</info> ' , $ this ->assetsFilesystem -> getDestinationPathString ( )));
138
113
$ manifest = [];
139
114
foreach ($ allAssets as $ asset ) {
140
- // $asset->getPublicPath() will start with a "/"
141
- $ targetPath = $ publicDir .$ asset ->publicPath ;
142
-
143
- if (!is_dir ($ dir = \dirname ($ targetPath ))) {
144
- $ this ->filesystem ->mkdir ($ dir );
145
- }
146
-
147
- $ this ->filesystem ->dumpFile ($ targetPath , $ asset ->content );
115
+ $ this ->assetsFilesystem ->write ($ asset ->publicPath , $ asset ->content );
148
116
$ manifest [$ asset ->logicalPath ] = $ asset ->publicPath ;
149
117
}
150
118
ksort ($ manifest );
0 commit comments