44
55namespace OpenForgeProject \MageForge \Console \Command \Hyva ;
66
7- use Laravel \Prompts \SelectPrompt ;
87use OpenForgeProject \MageForge \Console \Command \AbstractCommand ;
9- use OpenForgeProject \MageForge \Model \ThemeList ;
10- use OpenForgeProject \MageForge \Model \ThemePath ;
11- use OpenForgeProject \MageForge \Service \EnvironmentService ;
128use OpenForgeProject \MageForge \Service \HyvaTokens \ConfigReader ;
139use OpenForgeProject \MageForge \Service \HyvaTokens \TokenProcessor ;
14- use OpenForgeProject \MageForge \Service \ThemeBuilder \ HyvaThemes \ Builder as HyvaBuilder ;
10+ use OpenForgeProject \MageForge \Service \ThemeSelectionService ;
1511use Symfony \Component \Console \Command \Command ;
1612use Symfony \Component \Console \Input \InputArgument ;
1713use Symfony \Component \Console \Input \InputInterface ;
2319class TokensCommand extends AbstractCommand
2420{
2521 /**
26- * @param ThemePath $themePath
27- * @param ThemeList $themeList
22+ * @param ThemeSelectionService $themeSelectionService
2823 * @param TokenProcessor $tokenProcessor
29- * @param HyvaBuilder $hyvaBuilder
3024 * @param ConfigReader $configReader
31- * @param EnvironmentService $environmentService
3225 */
3326 public function __construct (
34- private readonly ThemePath $ themePath ,
35- private readonly ThemeList $ themeList ,
27+ private readonly ThemeSelectionService $ themeSelectionService ,
3628 private readonly TokenProcessor $ tokenProcessor ,
37- private readonly HyvaBuilder $ hyvaBuilder ,
3829 private readonly ConfigReader $ configReader ,
39- private readonly EnvironmentService $ environmentService ,
4030 ) {
4131 parent ::__construct ();
4232 }
@@ -64,7 +54,7 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
6454
6555 // If no theme code provided, select interactively
6656 if (empty ($ themeCode )) {
67- $ themeCode = $ this ->selectThemeInteractively ( $ output );
57+ $ themeCode = $ this ->themeSelectionService -> selectHyvaTheme ( $ this -> io );
6858 if ($ themeCode === null ) {
6959 return Command::FAILURE ;
7060 }
@@ -80,84 +70,6 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
8070 return $ this ->processTokens ($ themeCode , $ themePath );
8171 }
8272
83- /**
84- * Select theme interactively
85- *
86- * @param OutputInterface $output
87- * @return string|null
88- */
89- private function selectThemeInteractively (OutputInterface $ output ): ?string
90- {
91- $ hyvaThemes = $ this ->getHyvaThemes ();
92-
93- if (empty ($ hyvaThemes )) {
94- $ this ->io ->error ('No Hyvä themes found in this installation. ' );
95- return null ;
96- }
97-
98- // Check if we're in an interactive terminal environment
99- if (!$ this ->environmentService ->isInteractiveTerminal ()) {
100- $ this ->displayAvailableThemes ($ hyvaThemes );
101- return null ;
102- }
103-
104- return $ this ->promptForTheme ($ hyvaThemes );
105- }
106-
107- /**
108- * Display available themes for non-interactive environments
109- *
110- * @param array $hyvaThemes
111- * @return void
112- */
113- private function displayAvailableThemes (array $ hyvaThemes ): void
114- {
115- $ this ->io ->info ('Available Hyvä themes: ' );
116- foreach ($ hyvaThemes as $ theme ) {
117- $ this ->io ->writeln (' - ' . $ theme ->getCode ());
118- }
119- $ this ->io ->newLine ();
120- $ this ->io ->info ('Usage: bin/magento mageforge:hyva:tokens <theme-code> ' );
121- }
122-
123- /**
124- * Prompt user to select a theme
125- *
126- * @param array $hyvaThemes
127- * @return string|null
128- */
129- private function promptForTheme (array $ hyvaThemes ): ?string
130- {
131- $ options = [];
132- foreach ($ hyvaThemes as $ theme ) {
133- $ options [] = $ theme ->getCode ();
134- }
135-
136- // Set environment variables for Laravel Prompts
137- $ this ->environmentService ->setPromptEnvironment ();
138-
139- $ themeCodePrompt = new SelectPrompt (
140- label: 'Select Hyvä theme to generate tokens for ' ,
141- options: $ options ,
142- hint: 'Arrow keys to navigate, Enter to confirm '
143- );
144-
145- try {
146- $ selectedTheme = $ themeCodePrompt ->prompt ();
147- \Laravel \Prompts \Prompt::terminal ()->restoreTty ();
148-
149- // Reset environment
150- $ this ->environmentService ->resetPromptEnvironment ();
151-
152- return $ selectedTheme ;
153- } catch (\Exception $ e ) {
154- // Reset environment on exception
155- $ this ->environmentService ->resetPromptEnvironment ();
156- $ this ->io ->error ('Interactive mode failed: ' . $ e ->getMessage ());
157- return null ;
158- }
159- }
160-
16173 /**
16274 * Validate theme exists and is a Hyva theme
16375 *
@@ -166,16 +78,10 @@ private function promptForTheme(array $hyvaThemes): ?string
16678 */
16779 private function validateTheme (string $ themeCode ): ?string
16880 {
169- // Get theme path
170- $ themePath = $ this ->themePath -> getPath ($ themeCode );
81+ // Validate theme
82+ $ themePath = $ this ->themeSelectionService -> validateTheme ($ themeCode, true );
17183 if ($ themePath === null ) {
172- $ this ->io ->error ("Theme $ themeCode is not installed. " );
173- return null ;
174- }
175-
176- // Verify this is a Hyva theme
177- if (!$ this ->hyvaBuilder ->detect ($ themePath )) {
178- $ this ->io ->error ("Theme $ themeCode is not a Hyvä theme. This command only works with Hyvä themes. " );
84+ $ this ->io ->error ("Theme $ themeCode is not installed or is not a Hyvä theme. " );
17985 return null ;
18086 }
18187
@@ -276,24 +182,4 @@ private function handleFailure(array $result): int
276182JSON );
277183 return Command::FAILURE ;
278184 }
279-
280- /**
281- * Get list of Hyva themes
282- *
283- * @return array
284- */
285- private function getHyvaThemes (): array
286- {
287- $ allThemes = $ this ->themeList ->getAllThemes ();
288- $ hyvaThemes = [];
289-
290- foreach ($ allThemes as $ theme ) {
291- $ themePath = $ this ->themePath ->getPath ($ theme ->getCode ());
292- if ($ themePath && $ this ->hyvaBuilder ->detect ($ themePath )) {
293- $ hyvaThemes [] = $ theme ;
294- }
295- }
296-
297- return $ hyvaThemes ;
298- }
299185}
0 commit comments