PluginEnableCommand.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Webman\Console\Commands;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. class PluginEnableCommand extends Command
  8. {
  9. protected static $defaultName = 'plugin:enable';
  10. protected static $defaultDescription = 'Enable plugin by name';
  11. /**
  12. * @return void
  13. */
  14. protected function configure()
  15. {
  16. $this->addArgument('name', InputArgument::REQUIRED, 'Plugin name, for example foo/my-admin');
  17. }
  18. /**
  19. * @param InputInterface $input
  20. * @param OutputInterface $output
  21. * @return int
  22. */
  23. protected function execute(InputInterface $input, OutputInterface $output): int
  24. {
  25. $name = $input->getArgument('name');
  26. $output->writeln("Enable plugin $name");
  27. if (!strpos($name, '/')) {
  28. $output->writeln('<error>Bad name, name must contain character \'/\' , for example foo/MyAdmin</error>');
  29. return self::FAILURE;
  30. }
  31. $config_file = config_path() . "/plugin/$name/app.php";
  32. if (!is_file($config_file)) {
  33. $output->writeln("<error>$config_file not found</error>");
  34. return self::FAILURE;
  35. }
  36. $config = include $config_file;
  37. if (!isset($config['enable'])) {
  38. $output->writeln("<error>Config key 'enable' not found</error>");
  39. return self::FAILURE;
  40. }
  41. if ($config['enable']) {
  42. return self::SUCCESS;
  43. }
  44. $config_content = file_get_contents($config_file);
  45. $config_content = preg_replace('/(\'enable\' *?=> *?)(false)/', '$1true', $config_content);
  46. file_put_contents($config_file, $config_content);
  47. return self::SUCCESS;
  48. }
  49. }