|
3 | 3 |
|
4 | 4 | class VaahArtisan{ |
5 | 5 |
|
| 6 | + public $params; |
| 7 | + |
6 | 8 | public function __construct() |
7 | 9 | { |
8 | 10 |
|
9 | 11 |
|
10 | 12 |
|
11 | 13 | } |
12 | 14 |
|
13 | | - public function test() |
| 15 | + //------------------------------------------------- |
| 16 | + public function validateMigrateCommand($command) |
| 17 | + { |
| 18 | + //acceptable commands |
| 19 | + $commands = [ |
| 20 | + "migrate", |
| 21 | + "migrate:fresh", |
| 22 | + "migrate:install", |
| 23 | + "migrate:refresh", |
| 24 | + "migrate:reset", |
| 25 | + "migrate:rollback", |
| 26 | + "migrate:status", |
| 27 | + "db:seed", |
| 28 | + "db:wipe", |
| 29 | + ]; |
| 30 | + |
| 31 | + |
| 32 | + if(!in_array($command, $commands)) |
| 33 | + { |
| 34 | + $response['status'] = 'failed'; |
| 35 | + $response['errors'][] = 'Invalid command'; |
| 36 | + if(env('APP_DEBUG')) |
| 37 | + { |
| 38 | + $response['hint']['acceptable_commands'] = $commands; |
| 39 | + } |
| 40 | + return $response; |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + $response['status'] = 'success'; |
| 45 | + $response['data'][] = ''; |
| 46 | + return $response; |
| 47 | + } |
| 48 | + //------------------------------------------------- |
| 49 | + public function migrate($command, $db_connection_name=null, $path=null ) |
| 50 | + { |
| 51 | + |
| 52 | + $is_valid = $this->validateMigrateCommand($command); |
| 53 | + |
| 54 | + if($is_valid['status'] == 'failed') |
| 55 | + { |
| 56 | + return $is_valid; |
| 57 | + } |
| 58 | + |
| 59 | + if($path) |
| 60 | + { |
| 61 | + $this->params['--path'] = $path; |
| 62 | + } |
| 63 | + |
| 64 | + if($path) |
| 65 | + { |
| 66 | + $this->params['--database'] = $db_connection_name; |
| 67 | + } |
| 68 | + |
| 69 | + $response = [ |
| 70 | + 'status' => 'success' |
| 71 | + ]; |
| 72 | + |
| 73 | + try{ |
| 74 | + \Artisan::call($command, $this->params); |
| 75 | + $response['status'] = 'success'; |
| 76 | + $response['data'] = []; |
| 77 | + $response['messages'][] = 'Migrate command "'.$command.'" successfully executed'; |
| 78 | + |
| 79 | + }catch(\Exception $e) |
| 80 | + { |
| 81 | + $response['status'] = 'failed'; |
| 82 | + $response['errors'][] = $e->getMessage(); |
| 83 | + } |
| 84 | + return $response; |
| 85 | + |
| 86 | + } |
| 87 | + //------------------------------------------------- |
| 88 | + public function validateSeedCommand($command) |
| 89 | + { |
| 90 | + //acceptable commands |
| 91 | + $commands = [ |
| 92 | + "db:seed", |
| 93 | + "db:wipe", |
| 94 | + ]; |
| 95 | + |
| 96 | + |
| 97 | + if(!in_array($command, $commands)) |
| 98 | + { |
| 99 | + $response['status'] = 'failed'; |
| 100 | + $response['errors'][] = 'Invalid command'; |
| 101 | + if(env('APP_DEBUG')) |
| 102 | + { |
| 103 | + $response['hint']['acceptable_commands'] = $commands; |
| 104 | + } |
| 105 | + return $response; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + $response['status'] = 'success'; |
| 110 | + $response['data'][] = ''; |
| 111 | + return $response; |
| 112 | + } |
| 113 | + //------------------------------------------------- |
| 114 | + public function seed($command, $db_connection_name=null, $class=null) |
14 | 115 | { |
15 | | - die("<hr/>line number=abc"); |
| 116 | + $is_valid = $this->validateSeedCommand($command); |
| 117 | + |
| 118 | + if($is_valid['status'] == 'failed') |
| 119 | + { |
| 120 | + return $is_valid; |
| 121 | + } |
| 122 | + |
| 123 | + if($class) |
| 124 | + { |
| 125 | + $this->params['--class'] = $class; |
| 126 | + } |
| 127 | + |
| 128 | + if($db_connection_name) |
| 129 | + { |
| 130 | + $this->params['--database'] = $db_connection_name; |
| 131 | + } |
| 132 | + |
| 133 | + $response = [ |
| 134 | + 'status' => 'success' |
| 135 | + ]; |
| 136 | + |
| 137 | + try{ |
| 138 | + \Artisan::call($command, $this->params); |
| 139 | + $response['status'] = 'success'; |
| 140 | + $response['data'] = []; |
| 141 | + $response['messages'][] = 'Database has been seeded successfully'; |
| 142 | + |
| 143 | + }catch(\Exception $e) |
| 144 | + { |
| 145 | + $response['status'] = 'failed'; |
| 146 | + $response['errors'][] = $e->getMessage(); |
| 147 | + } |
| 148 | + return $response; |
| 149 | + |
16 | 150 | } |
| 151 | + //------------------------------------------------- |
17 | 152 |
|
18 | 153 | } |
0 commit comments