1 | <?php |
||
2 | /* |
||
3 | * This file is part of the Divergence package. |
||
4 | * |
||
5 | * (c) Henry Paradiz <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | namespace Divergence\CLI\Controllers\Commands; |
||
11 | |||
12 | use Divergence\CLI\Env; |
||
13 | use Divergence\CLI\Command; |
||
14 | use Divergence\CLI\ConfigWriter; |
||
15 | use Divergence\CLI\Controllers\Commands\Database; |
||
16 | |||
17 | /* |
||
18 | * @package Divergence\CLI |
||
19 | * @author Henry Paradiz <[email protected]> |
||
20 | */ |
||
21 | |||
22 | class Initialize |
||
23 | { |
||
24 | public static function init() |
||
25 | { |
||
26 | $climate = Command::getClimate(); |
||
27 | |||
28 | if (!Env::$hasComposer) { |
||
29 | $climate->backgroundYellow()->black()->out("No composer.json detected."); |
||
30 | $climate->backgroundYellow()->black()->out('Run composer init first!'); |
||
31 | return; |
||
32 | } |
||
33 | |||
34 | if (Env::$isRequired) { |
||
35 | $climate->info('divergence/divergence is already in your composer.json require.'); |
||
36 | } else { |
||
37 | $climate->info('divergence/divergence is not in your composer.json require.'); |
||
38 | $input = $climate->confirm('Do you want to run composer require divergence/divergence for this project?'); |
||
39 | $input->defaultTo('y'); |
||
40 | |||
41 | if ($input->confirmed()) { |
||
42 | shell_exec("composer require divergence/divergence --ansi"); |
||
43 | Env::getEnvironment(); // force recheck |
||
44 | } |
||
45 | } |
||
46 | |||
47 | static::initDirectories(); |
||
48 | |||
49 | static::initAutoloader(); |
||
50 | |||
51 | static::initDatabase(); |
||
52 | } |
||
53 | |||
54 | /* |
||
55 | * Copies default bootstrap files from the framework's |
||
56 | * vendor directory if they aren't already there. |
||
57 | * |
||
58 | */ |
||
59 | public static function initDirectories() |
||
60 | { |
||
61 | $climate = Command::getClimate(); |
||
62 | |||
63 | $freshInstall = true; |
||
64 | |||
65 | $requiredFiles = [ |
||
66 | 'bootstrap/app.php', |
||
67 | 'bootstrap/autoload.php', |
||
68 | 'bootstrap/router.php', |
||
69 | 'config/app.php', |
||
70 | 'config/db.php', |
||
71 | 'public/index.php', |
||
72 | 'public/.htaccess', |
||
73 | 'views/dwoo/design.tpl', |
||
74 | ]; |
||
75 | |||
76 | foreach ($requiredFiles as $file) { |
||
77 | if (!file_exists(getcwd().'/'.$file)) { |
||
78 | $climate->error($file.' missing.'); |
||
79 | } else { |
||
80 | $freshInstall = false; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | if ($freshInstall) { |
||
85 | $climate->info('Looks like this is a fresh install'); |
||
86 | $input = $climate->confirm('Do you want to bootstrap this project with framework defaults?'); |
||
87 | |||
88 | $input->defaultTo('y'); |
||
89 | |||
90 | if ($input->confirmed()) { |
||
91 | $climate->info('Creating directories...'); |
||
92 | foreach ($requiredFiles as $file) { |
||
93 | $source = 'vendor/divergence/divergence/'.$file; |
||
94 | $dest = getcwd().'/'.$file; |
||
95 | if (!file_exists(dirname($dest))) { |
||
96 | mkdir(dirname($dest), 0777, true); |
||
97 | } |
||
98 | $climate->info($dest); |
||
99 | copy($source, $dest); |
||
100 | } |
||
101 | $freshInstall = false; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
102 | } |
||
103 | } else { |
||
104 | $climate->info('Looks like this project has already been bootstrapped.'); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /* |
||
109 | * |
||
110 | * Adds a PSR-4 namespace to composer.json |
||
111 | * Asks if you want to run `composer install` and runs it |
||
112 | * Runs Env::getEnvironment() once to make sure it was succesful |
||
113 | */ |
||
114 | public static function installAutoloader() |
||
115 | { |
||
116 | $climate = Command::getClimate(); |
||
117 | $suggestedName = explode('/', Env::$package['name'])[1]; |
||
118 | $input = $climate->confirm('Do you want to create a namespace called '.$suggestedName.' mapped to directory src?'); |
||
119 | $input->defaultTo('y'); |
||
120 | if ($input->confirmed()) { |
||
121 | Env::$package['autoload']['psr-4'][$suggestedName."\\"] = 'src/'; |
||
122 | Env::setPKG(getcwd(), Env::$package); |
||
123 | Env::getEnvironment(); |
||
124 | |||
125 | $input = $climate->confirm('Run composer install to register new autoloaded folder?'); |
||
126 | $input->defaultTo('y'); |
||
127 | if ($input->confirmed()) { |
||
128 | shell_exec('composer install --ansi'); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /* |
||
134 | * Checks autoloader config for a namespace to use. |
||
135 | * Installs one if none found. |
||
136 | * Asks to use if only one found. |
||
137 | * TODO: prompt to select if more than |
||
138 | * Sets Env::$namespace once it's found. |
||
139 | */ |
||
140 | public static function initAutoloader() |
||
141 | { |
||
142 | $climate = Command::getClimate(); |
||
143 | |||
144 | $autoloaders = Env::$autoloaders; |
||
145 | |||
146 | if (!count($autoloaders)) { |
||
147 | $climate->info('No local autoloaded directory found!'); |
||
148 | static::installAutoloader(); |
||
149 | $autoloaders = Env::$autoloaders; |
||
150 | } |
||
151 | |||
152 | // if only one autoloader ask if we should use this to initialize |
||
153 | if (count($autoloaders) === 1) { |
||
154 | $key = array_keys($autoloaders)[0]; |
||
155 | $climate->info(sprintf('Found a single autoloaded namespace: <bold><yellow>%s</yellow></bold> => loaded from <yellow>./%s</yellow>', $key, $autoloaders[$key])); |
||
156 | $input = $climate->confirm('Initialize at this namespace?'); |
||
157 | |||
158 | $input->defaultTo('y'); |
||
159 | |||
160 | if ($input->confirmed()) { |
||
161 | Env::$namespace = $key; |
||
162 | } |
||
163 | } elseif (count($autoloaders) > 1) { |
||
164 | // prompt: found count($autoloaders) autoloader configs. Which one is your namespace? |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /* |
||
169 | * Detects which database configs are default and starts a wizard for them |
||
170 | * Only runs wizard for mysql and dev-mysql during init |
||
171 | */ |
||
172 | public static function initDatabase() |
||
173 | { |
||
174 | $climate = Command::getClimate(); |
||
175 | $config = Env::getConfig(getcwd(), 'db'); |
||
176 | $defaults = require getcwd().'/vendor/divergence/divergence/config/db.php'; |
||
177 | foreach ($config as $label=>$dbconf) { |
||
178 | if ($dbconf === $defaults[$label]) { |
||
179 | if (in_array($label, ['mysql','dev-mysql'])) { |
||
180 | $climate->info(sprintf('Detected default database config %s', $label)); |
||
181 | $input = $climate->confirm('Build database config <bold><yellow>'.$label.'</yellow></bold>?'); |
||
182 | $input->defaultTo('y'); |
||
183 | $thisConfig = $defaults[$label]; // derive default |
||
184 | $thisConfig['database'] = $thisConfig['username'] = explode('/', Env::$package['name'])[1]; // set default |
||
185 | if ($input->confirmed()) { |
||
186 | $thisConfig = Database::wizard($thisConfig); |
||
187 | $input = $climate->confirm('Save this config?'); |
||
188 | $input->defaultTo('y'); |
||
189 | if ($input->confirmed()) { |
||
190 | ConfigWriter::configWriter($label, $thisConfig); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | } // if |
||
195 | } // foreach |
||
196 | } |
||
197 | } |
||
198 |