EPESI BIM • Business Information Manager • Cloud CRM/ERP http://epe.si/ https://epe.si
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveAllCommand.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Epesi\Console;
  3. use DB;
  4. use Cache;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class RemoveAllCommand extends Command
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('remove:all')
  14. ->setDescription('Truncate database and remove all data from EPESI')
  15. ;
  16. }
  17. protected function execute(InputInterface $input, OutputInterface $output)
  18. {
  19. $dir = __DIR__ . '/../data/';
  20. if(is_dir($dir)) {
  21. $this->truncateDir($output, $dir);
  22. }
  23. else {
  24. $output->writeln('Data directory doesn\'t exist or you don\'t have required permissions');
  25. }
  26. $this->truncateDb($output);
  27. $output->writeln('EPESI removed. Please install application again');
  28. }
  29. private function truncateDir($output, $dir) {
  30. $files = array_diff(scandir($dir), ['.', '..']);
  31. foreach ($files as $file) {
  32. if(!is_dir("$dir/$file")) unlink("$dir/$file");
  33. else {
  34. $handle = opendir("$dir/$file");
  35. closedir($handle);
  36. exec('rm -rf '."$dir/$file");
  37. }
  38. }
  39. $output->writeln('Data folder cleared!');
  40. }
  41. private function truncateDb ($output) {
  42. Cache::clear();
  43. DB::Execute('SET FOREIGN_KEY_CHECKS=0;');
  44. foreach(DB::MetaTables() as $k => $v) {
  45. DB::DropTable($v);
  46. }
  47. DB::Execute('SET FOREIGN_KEY_CHECKS=1;');
  48. $output->writeln('Database truncated!');
  49. }
  50. }