12
12
namespace App \Service \Toolkit ;
13
13
14
14
use App \Enum \ToolkitKitId ;
15
+ use App \Util \SourceCleaner ;
15
16
use Symfony \Component \DependencyInjection \Attribute \Autowire ;
17
+ use Symfony \Component \Filesystem \Path ;
16
18
use Symfony \Component \HttpFoundation \UriSigner ;
17
19
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
18
20
use Symfony \UX \Toolkit \Asset \Component ;
21
+ use Symfony \UX \Toolkit \Installer \PoolResolver ;
19
22
use Symfony \UX \Toolkit \Kit \Kit ;
20
23
use Symfony \UX \Toolkit \Registry \RegistryFactory ;
21
24
use function Symfony \Component \String \s ;
@@ -77,6 +80,41 @@ public function renderComponentPreviewCodeTabs(ToolkitKitId $kitId, string $code
77
80
]);
78
81
}
79
82
83
+ public function renderInstallationSteps (ToolkitKitId $ kitId , Component $ component ): string
84
+ {
85
+ $ kit = $ this ->getKit ($ kitId );
86
+ $ pool = (new PoolResolver )->resolveForComponent ($ kit , $ component );
87
+
88
+ $ manual = '<p>The UX Toolkit is not mandatory to install a component. You can install it manually by following the next steps:</p> ' ;
89
+ $ manual .= '<ol style="display: grid; gap: 1rem;"> ' ;
90
+ $ manual .= '<li><strong>Copy the files into your Symfony app:</strong> ' ;
91
+ foreach ($ pool ->getFiles () as $ file ) {
92
+ $ manual .= sprintf (
93
+ "<details><summary><code>%s</code></summary> \n%s \n</details> " ,
94
+ $ file ->relativePathNameToKit ,
95
+ sprintf ("\n```%s \n%s \n``` " , pathinfo ($ file ->relativePathNameToKit , PATHINFO_EXTENSION ), trim (file_get_contents (Path::join ($ kit ->path , $ file ->relativePathNameToKit ))))
96
+ );
97
+ }
98
+ $ manual .= '</li> ' ;
99
+
100
+ if ($ phpPackageDependencies = $ pool ->getPhpPackageDependencies ()) {
101
+ $ manual .= '<li><strong>If necessary, install the following Composer dependencies:</strong> ' ;
102
+ $ manual .= self ::generateTerminal ('shell ' , SourceCleaner::processTerminalLines ('composer require ' . implode (' ' , $ phpPackageDependencies )));
103
+ $ manual .= '</li> ' ;
104
+ }
105
+
106
+ $ manual .= '<li><strong>And the most important, enjoy!</strong></li> ' ;
107
+ $ manual .= '</ol> ' ;
108
+
109
+ return $ this ->generateTabs ([
110
+ 'Automatic ' => sprintf (
111
+ '<p>Ensure the Symfony UX Toolkit is installed in your Symfony app:</p>%s<p>Then, run the following command to install the component and its dependencies:</p>%s ' ,
112
+ self ::generateTerminal ('shell ' , SourceCleaner::processTerminalLines ('composer require --dev symfony/ux-toolkit ' ), 'margin-bottom: 1rem ' ),
113
+ self ::generateTerminal ('shell ' , SourceCleaner::processTerminalLines ("bin/console ux:toolkit:install-component {$ component ->name } --kit {$ kitId ->value }" ), 'margin-bottom: 1rem ' ),
114
+ ),
115
+ 'Manual ' => $ manual ,
116
+ ]);
117
+ }
80
118
81
119
/**
82
120
* @param non-empty-array<string, string> $tabs
@@ -103,4 +141,17 @@ private static function generateTabs(array $tabs): string
103
141
</div>
104
142
HTML ;
105
143
}
144
+
145
+ private static function generateTerminal (string $ language , string $ content , string $ style = '' ): string
146
+ {
147
+ return <<<HTML
148
+ <div class="Terminal terminal-code" style=" $ style">
149
+ <div class="Terminal_body">
150
+ <div class="Terminal_content">
151
+ <pre><code class="language- {$ language }"> {$ content }</code></pre>
152
+ </div>
153
+ </div>
154
+ </div>
155
+ HTML ;
156
+ }
106
157
}
0 commit comments