1212namespace App \Service \Toolkit ;
1313
1414use App \Enum \ToolkitKitId ;
15+ use App \Util \SourceCleaner ;
1516use Symfony \Component \DependencyInjection \Attribute \Autowire ;
17+ use Symfony \Component \Filesystem \Path ;
1618use Symfony \Component \HttpFoundation \UriSigner ;
1719use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
1820use Symfony \UX \Toolkit \Asset \Component ;
21+ use Symfony \UX \Toolkit \Installer \PoolResolver ;
1922use Symfony \UX \Toolkit \Kit \Kit ;
2023use Symfony \UX \Toolkit \Registry \RegistryFactory ;
2124use function Symfony \Component \String \s ;
@@ -77,6 +80,41 @@ public function renderComponentPreviewCodeTabs(ToolkitKitId $kitId, string $code
7780 ]);
7881 }
7982
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+ }
80118
81119 /**
82120 * @param non-empty-array<string, string> $tabs
@@ -103,4 +141,17 @@ private static function generateTabs(array $tabs): string
103141</div>
104142HTML ;
105143 }
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+ }
106157}
0 commit comments