Licitator 1.0
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.

120 lines
5.1 KiB

5 years ago
  1. <?php
  2. namespace Prometeo\CommandsBundle\Commands;
  3. use Symfony\Component\Console\Command\Command as ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. class TemplateToFiles extends ContainerAwareCommand
  12. {
  13. private $em;
  14. public function __construct(ContainerInterface $container)
  15. {
  16. parent::__construct();
  17. $this->em = $container->get('doctrine')->getManager();
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function configure(): void
  23. {
  24. $this->setName('prometeo:databasetemplate:files');
  25. $this->addArgument(
  26. 'folder',
  27. InputArgument::OPTIONAL,
  28. 'File with routes to capture'
  29. )
  30. ;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function execute(InputInterface $input, OutputInterface $output): void
  36. {
  37. $name = $input->getArgument('folder');
  38. /** @var KernelInterface $kernel */
  39. $kernel = $this->getApplication()->getKernel();
  40. $rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
  41. $templatesDir=$rootDir.'/../templates/database/templates/';
  42. $templatesPdfDir=$rootDir.'/../templates/database/templates/pdf/';
  43. $templatesMailDir=$rootDir.'/../templates/database/templates/mail/';
  44. $externalsDir=$rootDir.'/../templates/database/externals/';
  45. $componentDir=$rootDir.'/../templates/database/components/';
  46. //Comprobar si existe el directorio database, sino crearlo.
  47. $output->writeln('Comporbando directorio-->'.$rootDir.'/../templates/database'."\n" );
  48. if (!is_dir($dir = $rootDir.'/../templates/database')) {
  49. mkdir($rootDir.'/../templates/database');
  50. }
  51. if (!is_dir($dir = $rootDir.'/../templates/database/templates')) {
  52. mkdir($rootDir.'/../templates/database/templates');
  53. }
  54. if (!is_dir($dir = $rootDir.'/../templates/database/templates/pdf')) {
  55. mkdir($rootDir.'/../templates/database/templates/pdf');
  56. }
  57. if (!is_dir($dir = $rootDir.'/../templates/database/templates/mail')) {
  58. mkdir($rootDir.'/../templates/database/templates/mail');
  59. }
  60. if (!is_dir($dir = $rootDir.'/../templates/database/externals')) {
  61. mkdir($rootDir.'/../templates/database/externals');
  62. }
  63. if (!is_dir($dir = $rootDir.'/../templates/database/components')) {
  64. mkdir($rootDir.'/../templates/database/components');
  65. }
  66. $templates = $this->em->getRepository('App:Template')->findAll();
  67. $externals = $this->em->getRepository('App:External')->findAll();
  68. $components = $this->em->getRepository('App:Component')->findAll();
  69. foreach($templates as $template){
  70. $this->makeFolders($template->getFilename(),$templatesDir );
  71. file_put_contents($templatesDir.$template->getFilename(), $template->getStyle().$template->getJavascript().$template->getSource() );
  72. $output->writeln('Creada plantilla-->'.$templatesDir.$template->getFilename()."\n" );
  73. if(!empty($template->getPdffilename())){
  74. $this->makeFolders($template->getPdffilename(),$templatesPdfDir );
  75. file_put_contents($templatesPdfDir.$template->getPdffilename(), $template->getPdftwig() );
  76. $output->writeln('Creada plantilla-->'.$templatesPdfDir.$template->getPdffilename()."\n" );
  77. }
  78. if(!empty($template->getEmailfilename())){
  79. $this->makeFolders($template->getEmailfilename(),$templatesMailDir );
  80. file_put_contents($templatesMailDir.$template->getEmailfilename(), $template->getEmailtwig() );
  81. $output->writeln('Creada plantilla-->'.$templatesMailDir.$template->getEmailfilename()."\n" );
  82. }
  83. }
  84. foreach($externals as $external){
  85. $this->makeFolders($external->getFilename(),$externalsDir );
  86. file_put_contents($externalsDir.$external->getFilename(), $external->getSource() );
  87. $output->writeln('Creada plantilla-->'.$externalsDir.$external->getFilename()."\n" );
  88. }
  89. foreach($components as $component){
  90. $this->makeFolders($component->getFilename(),$componentDir );
  91. file_put_contents($componentDir.$component->getFilename(), $component->getSource() );
  92. $output->writeln('Creada plantilla-->'.$componentDir.$component->getFilename()."\n" );
  93. }
  94. $output->writeln($name );
  95. }
  96. public function makeFolders($filepath, $base){
  97. $directories = explode( '/', $filepath );
  98. $file = array_pop( $directories );
  99. foreach( $directories as $dir )
  100. {
  101. $path = sprintf( '%s/%s', $base, $dir );
  102. if(!is_dir($path)){
  103. mkdir( $path );
  104. chmod( $path, 777 );
  105. }
  106. $base = $path;
  107. }
  108. }
  109. }