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