|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codenom\Handlers\Commands; |
| 4 | + |
| 5 | +use CodeIgniter\CLI\BaseCommand; |
| 6 | +use CodeIgniter\CLI\CLI; |
| 7 | + |
| 8 | +class HandlersRegister extends BaseCommand |
| 9 | +{ |
| 10 | + protected $group = 'Handlers'; |
| 11 | + protected $name = 'handlers:register'; |
| 12 | + protected $description = 'Locate supported handlers and add them to the database'; |
| 13 | + protected $usage = 'handlers:register'; |
| 14 | + protected $arguments = []; |
| 15 | + |
| 16 | + public function run(array $params = []) |
| 17 | + { |
| 18 | + // Load the library |
| 19 | + $lib = service('handlers'); |
| 20 | + |
| 21 | + // Fetch all supported configs |
| 22 | + $configs = $lib->findConfigs(); |
| 23 | + if (empty($configs)) : |
| 24 | + CLI::write('WARNING: No handler config files detected!', 'yellow'); |
| 25 | + return; |
| 26 | + endif; |
| 27 | + |
| 28 | + // Process each handler |
| 29 | + foreach ($configs as $configClass) : |
| 30 | + |
| 31 | + // Scan for supported handlers |
| 32 | + $handlers = $lib->findHandlers($configClass); |
| 33 | + if (empty($handlers)) : |
| 34 | + // Check for errors |
| 35 | + if ($errors = $lib->getErrors()) : |
| 36 | + foreach ($errors as $error) : |
| 37 | + CLI::write($error, 'red'); |
| 38 | + endforeach; |
| 39 | + else : |
| 40 | + CLI::write('No handlers detected for config file: ' . $configClass, 'yellow'); |
| 41 | + endif; |
| 42 | + |
| 43 | + continue; |
| 44 | + endif; |
| 45 | + |
| 46 | + // Get an instance of the model |
| 47 | + $config = new $configClass(); |
| 48 | + $model = new $config->model(); |
| 49 | + |
| 50 | + // Load each handler |
| 51 | + foreach ($handlers as $handlerClass) : |
| 52 | + |
| 53 | + // Get the attributes from the handler itself |
| 54 | + $handler = new $handlerClass(); |
| 55 | + $row = $handler->attributes; |
| 56 | + $row['class'] = $handlerClass; |
| 57 | + |
| 58 | + // Check for an existing registration |
| 59 | + if ($existing = $model->where('uid', $row['uid'])->first()) : |
| 60 | + // Update it |
| 61 | + $model->update($existing->id, $row); |
| 62 | + else : |
| 63 | + // Create a new registration |
| 64 | + $handlerId = $model->insert($row); |
| 65 | + CLI::write("New handler registered for {$configClass}: {$handlerClass}", 'green'); |
| 66 | + endif; |
| 67 | + |
| 68 | + endforeach; |
| 69 | + |
| 70 | + endforeach; |
| 71 | + |
| 72 | + $this->call('handlers:list'); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** End of src/Commands/HandlersRegister.php */ |
0 commit comments