consigliere /
components
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Consigliere\Components\Commands; |
||
| 4 | |||
| 5 | use Illuminate\Console\Command as ComponentCommand; |
||
| 6 | use Illuminate\Support\Str; |
||
| 7 | use RuntimeException; |
||
| 8 | use Consigliere\Components\Component; |
||
| 9 | use Consigliere\Components\Repository; |
||
| 10 | use Consigliere\Components\Traits\ComponentCommandTrait; |
||
| 11 | use Symfony\Component\Console\Input\InputArgument; |
||
| 12 | use Symfony\Component\Console\Input\InputOption; |
||
| 13 | |||
| 14 | class SeedCommand extends ComponentCommand |
||
| 15 | { |
||
| 16 | use ComponentCommandTrait; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The console command name. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $name = 'component:seed'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The console command description. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $description = 'Run database seeder from the specified component or from all components.'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Execute the console command. |
||
| 34 | * |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | public function fire() |
||
| 38 | { |
||
| 39 | try { |
||
| 40 | if ($name = $this->argument('component')) { |
||
| 41 | $name = Str::studly($name); |
||
|
0 ignored issues
–
show
|
|||
| 42 | $this->componentSeed($this->getComponentByName($name)); |
||
| 43 | } else { |
||
| 44 | $components = $this->getComponentRepository()->getOrdered(); |
||
| 45 | array_walk($components, [$this, 'componentSeed']); |
||
| 46 | $this->info('All components seeded.'); |
||
| 47 | } |
||
| 48 | } catch (\Exception $e) { |
||
| 49 | $this->error($e->getMessage()); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @throws RuntimeException |
||
| 55 | * |
||
| 56 | * @return Repository |
||
| 57 | */ |
||
| 58 | public function getComponentRepository() |
||
| 59 | { |
||
| 60 | $components = $this->laravel['components']; |
||
| 61 | if (!$components instanceof Repository) { |
||
| 62 | throw new RuntimeException("Component repository not found!"); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $components; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param $name |
||
| 70 | * |
||
| 71 | * @throws RuntimeException |
||
| 72 | * |
||
| 73 | * @return Component |
||
| 74 | */ |
||
| 75 | public function getComponentByName($name) |
||
| 76 | { |
||
| 77 | $components = $this->getComponentRepository(); |
||
| 78 | if ($components->has($name) === false) { |
||
| 79 | throw new RuntimeException("Component [$name] does not exists."); |
||
| 80 | } |
||
| 81 | |||
| 82 | return $components->get($name); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param Component $component |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function componentSeed(Component $component) |
||
| 91 | { |
||
| 92 | $seeders = []; |
||
| 93 | $name = $component->getName(); |
||
| 94 | $config = $component->get('seed'); |
||
| 95 | if (is_array($config) && array_key_exists('seeds', $config)) { |
||
| 96 | foreach ((array)$config['seeds'] as $class) { |
||
| 97 | if (@class_exists($class)) { |
||
| 98 | $seeders[] = $class; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } else { |
||
| 102 | $class = $this->getSeederName($name); //legacy support |
||
| 103 | if (@class_exists($class)) { |
||
| 104 | $seeders[] = $class; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if (count($seeders) > 0) { |
||
| 109 | array_walk($seeders, [$this, 'dbSeed']); |
||
| 110 | $this->info("Component [$name] seeded."); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Seed the specified component. |
||
| 116 | * |
||
| 117 | * @param string $className |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | protected function dbSeed($className) |
||
| 122 | { |
||
| 123 | $params = [ |
||
| 124 | '--class' => $className, |
||
| 125 | ]; |
||
| 126 | |||
| 127 | if ($option = $this->option('database')) { |
||
| 128 | $params['--database'] = $option; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($option = $this->option('force')) { |
||
| 132 | $params['--force'] = $option; |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->call('db:seed', $params); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get master database seeder name for the specified component. |
||
| 140 | * |
||
| 141 | * @param string $name |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getSeederName($name) |
||
| 146 | { |
||
| 147 | $name = Str::studly($name); |
||
| 148 | |||
| 149 | $namespace = $this->laravel['components']->config('namespace'); |
||
| 150 | |||
| 151 | return $namespace . '\\' . $name . '\Database\Seeders\\' . $name . 'DatabaseSeeder'; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the console command arguments. |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | protected function getArguments() |
||
| 160 | { |
||
| 161 | return [ |
||
| 162 | ['component', InputArgument::OPTIONAL, 'The name of component will be used.'], |
||
| 163 | ]; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the console command options. |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | protected function getOptions() |
||
| 172 | { |
||
| 173 | return [ |
||
| 174 | ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'], |
||
| 175 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], |
||
| 176 | ]; |
||
| 177 | } |
||
| 178 | } |
||
| 179 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.