Application.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\CommandLine;
  3. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  4. use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
  5. use Symfony\Component\DependencyInjection\Tests\Compiler\DInterface;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class Application extends \Symfony\Component\Console\Application
  9. {
  10. protected $dir = '';
  11. /**
  12. * @override
  13. */
  14. public function run(InputInterface $input = null, OutputInterface $output = null)
  15. {
  16. $this->loadCommandsControllers($this->getCommands());
  17. parent::run($input, $output);
  18. }
  19. /**
  20. * Get all available files
  21. * @return array
  22. */
  23. protected function getCommands()
  24. {
  25. $files = glob(ModuleConstants::getFullPath('app', $this->dir) .DIRECTORY_SEPARATOR. '*.php');
  26. $commands = [];
  27. foreach ($files as $file)
  28. {
  29. $file = substr($file, strrpos($file, DIRECTORY_SEPARATOR) + 1);
  30. $file = substr($file, 0, strrpos($file, '.'));
  31. $commands[] = $file;
  32. }
  33. return $commands;
  34. }
  35. /**
  36. * Create new objects and add it
  37. * @param $commands
  38. */
  39. protected function loadCommandsControllers($commands)
  40. {
  41. $this->dir = str_replace('/', DIRECTORY_SEPARATOR, $this->dir);
  42. foreach ($commands as $command)
  43. {
  44. $class = ModuleConstants::getRootNamespace() . '\App\\' . $this->dir . '\\' . $command;
  45. $this->add(new $class);
  46. }
  47. }
  48. }