PluginDisableCommand.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 PluginDisableCommand extends Command
  8. {
  9. protected static $defaultName = 'plugin:disable';
  10. protected static $defaultDescription = 'Disable 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("Disable 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. return self::SUCCESS;
  34. }
  35. $config = include $config_file;
  36. if (empty($config['enable'])) {
  37. return self::SUCCESS;
  38. }
  39. $config_content = file_get_contents($config_file);
  40. $config_content = preg_replace('/(\'enable\' *?=> *?)(true)/', '$1false', $config_content);
  41. file_put_contents($config_file, $config_content);
  42. return self::SUCCESS;
  43. }
  44. }