-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandHandler.php
64 lines (53 loc) · 1.51 KB
/
CommandHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Core;
class CommandHandler{
/**
* Handle the incoming command
* @return void
*/
public function __construct($argv){
# locate the run php command & store
$command = $argv[1];
# run the propper command
switch ($command) {
case 'dev':
$this->dev();
break;
case 'db:migrate':
$this->migrateDatabase();
break;
default:
echo 'Unkown command!';
break;
}
}
/**
* Runs the dev server
* @return void
*/
private function dev(){
echo "Have a great dev experience! \n";
exec('php -S localhost:8000');
}
/**
* Run the sql database file
* @return void
*/
private function migrateDatabase(){
echo "Running database migration ..\n";
$command = "mysql -u {$_ENV['DB_USERNAME']} {$_ENV['DB_PASSWORD']} {$_ENV['DB_NAME']} < database.sql";
exec($command, $output, $returnVar);
$this->handleCommandOutput($returnVar, $output, 'Error migrating database', 'Database migrating succeded');
}
/**
* Handle a given command output
* @return void
*/
private function handleCommandOutput($returnVar, $output, $successMessage, $errorMessage){
if ($returnVar == 0) {
echo "$errorMessage \n" . implode("\n", $output);
} else {
echo "$successMessage\n";
}
}
}