1+ <?php
2+
3+ namespace HichemTabTech \LaravelFS \Console \Concerns ;
4+
5+ use Illuminate \Support \ProcessUtils ;
6+ use Illuminate \Support \Str ;
7+ use RuntimeException ;
8+ use Symfony \Component \Console \Input \InputInterface ;
9+ use Symfony \Component \Console \Output \OutputInterface ;
10+ use Symfony \Component \Process \PhpExecutableFinder ;
11+ use Symfony \Component \Process \Process ;
12+ use function Illuminate \Support \php_binary ;
13+ use function Laravel \Prompts \error ;
14+
15+ trait CommandsUtils
16+ {
17+
18+ private function getGlobalTemplatesPath (): string
19+ {
20+ // Determine OS-specific config directory
21+ if (windows_os ()) {
22+ $ configDir = (getenv ('APPDATA ' ) ?: $ _SERVER ['APPDATA ' ]) . '\laravelfs ' ;
23+ } else {
24+ $ configDir = (getenv ('XDG_CONFIG_HOME ' ) ?: $ _SERVER ['HOME ' ]) . '\.config\laravelfs ' ;
25+ }
26+
27+ return $ configDir . '\templates.json ' ;
28+ }
29+
30+ private function getSavedTemplates (bool $ noInteract = false ): array
31+ {
32+ // Get the global templates path
33+ $ configPath = $ this ->getGlobalTemplatesPath ();
34+ $ noTemplates = false ;
35+
36+ // Check if the template file exists
37+ if (!file_exists ($ configPath )) {
38+ $ noTemplates = true ;
39+ }
40+
41+ if (!$ noTemplates ) {
42+ // Load the templates
43+ $ templatesConfig = json_decode (file_get_contents ($ configPath ), true );
44+ $ templates = [];
45+
46+ if (empty ($ templatesConfig )) {
47+ $ noTemplates = true ;
48+ }
49+ elseif (empty ($ templatesConfig ['templates ' ])) {
50+ $ noTemplates = true ;
51+ }
52+ else {
53+ $ templates = $ templatesConfig ['templates ' ];
54+ }
55+
56+ }
57+
58+ if ($ noTemplates ) {
59+ if (!$ noInteract ) {
60+ error ('No templates found. Create one using `laravelfs template:new <template-name>` ' );
61+ }
62+ return [
63+ 'templates ' => [],
64+ 'path ' => $ configPath ,
65+ ];
66+ }
67+
68+ return [
69+ 'templates ' => $ templates ,
70+ 'path ' => $ configPath ,
71+ ];
72+ }
73+
74+ /**
75+ * Get the path to the appropriate PHP binary.
76+ *
77+ * @return string
78+ */
79+ protected function phpBinary (): string
80+ {
81+ $ phpBinary = function_exists ('Illuminate\Support\php_binary ' )
82+ ? php_binary ()
83+ : (new PhpExecutableFinder )->find (false );
84+
85+ return $ phpBinary !== false
86+ ? ProcessUtils::escapeArgument ($ phpBinary )
87+ : 'php ' ;
88+ }
89+
90+ /**
91+ * Run the given commands.
92+ *
93+ * @param array $commands
94+ * @param InputInterface $input
95+ * @param OutputInterface $output
96+ * @param string|null $workingPath
97+ * @param array $env
98+ * @return Process
99+ */
100+ protected function runCommands (array $ commands , InputInterface $ input , OutputInterface $ output , ?string $ workingPath = null , array $ env = []): Process
101+ {
102+ if (!$ output ->isDecorated ()) {
103+ $ commands = array_map (function ($ value ) {
104+ if (Str::startsWith ($ value , ['chmod ' , 'git ' , $ this ->phpBinary ().' ./vendor/bin/pest ' ])) {
105+ return $ value ;
106+ }
107+
108+ return $ value .' --no-ansi ' ;
109+ }, $ commands );
110+ }
111+
112+ if ($ input ->getOption ('quiet ' )) {
113+ $ commands = array_map (function ($ value ) {
114+ if (Str::startsWith ($ value , ['chmod ' , 'git ' , $ this ->phpBinary ().' ./vendor/bin/pest ' ])) {
115+ return $ value ;
116+ }
117+
118+ return $ value .' --quiet ' ;
119+ }, $ commands );
120+ }
121+
122+ $ process = Process::fromShellCommandline (implode (' && ' , $ commands ), $ workingPath , $ env , null , null );
123+
124+ if ('\\' !== DIRECTORY_SEPARATOR AND file_exists ('/dev/tty ' ) AND is_readable ('/dev/tty ' )) {
125+ try {
126+ $ process ->setTty (true );
127+ } catch (RuntimeException $ e ) {
128+ $ output ->writeln (' <bg=yellow;fg=black> WARN </> ' .$ e ->getMessage ().PHP_EOL );
129+ }
130+ }
131+
132+ $ process ->run (function ($ type , $ line ) use ($ output ) {
133+ $ output ->write (' ' .$ line );
134+ });
135+
136+ return $ process ;
137+ }
138+
139+ /**
140+ * Get the installation directory.
141+ *
142+ * @param string $name
143+ * @return string
144+ */
145+ protected function getInstallationDirectory (string $ name ): string
146+ {
147+ return $ name !== '. ' ? getcwd ().'/ ' .$ name : '. ' ;
148+ }
149+
150+ /**
151+ * Verify that the application does not already exist.
152+ *
153+ * @param string $directory
154+ * @return void
155+ */
156+ protected function verifyApplicationDoesntExist (string $ directory ): void
157+ {
158+ if ((is_dir ($ directory ) || is_file ($ directory )) AND $ directory != getcwd ()) {
159+ throw new RuntimeException ('Application already exists! ' );
160+ }
161+ }
162+ }
0 commit comments