InitDocsisTemplatesCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace CablemodemBundle\Command;
  3. use CablemodemBundle\Entity\CablemodemModel;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Finder\Finder;
  9. use TemplateBundle\Entity\Template;
  10. class InitDocsisTemplatesCommand extends ContainerAwareCommand
  11. {
  12. /**
  13. * @see Command
  14. */
  15. protected function configure()
  16. {
  17. $this
  18. ->setName('init:templates:docsis')
  19. ->setDescription('Init Docsis FD3 Templates For the CableModem Models')
  20. ->setDefinition(array(
  21. new InputArgument('path', InputArgument::OPTIONAL, "Directory of templates", realpath(__DIR__.'/../Resources/docsis-templates/')),
  22. ))
  23. ->setHelp(<<<EOT
  24. The <info>init:templates:docsis</info> command import all the template docsis configuration, including cablemodem models and their corresponding templates
  25. EOT
  26. );
  27. }
  28. /**
  29. * @param InputInterface $input
  30. * @param OutputInterface $output
  31. */
  32. protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. $path = $input->getArgument('path');
  35. $this->output = $output;
  36. $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
  37. $finder = new Finder();
  38. $finder->files()->name("*.ini")->in($path);
  39. foreach ($finder as $file) {
  40. $iniFile = $file->getRealpath();
  41. $config = parse_ini_file($iniFile, true);
  42. $this->output->writeln("Open Ini File <info>$iniFile</info>");
  43. foreach ($config as $section => $config) {
  44. $cablemodemModel = $this->em->getRepository('CablemodemBundle:CablemodemModel')->findOneByName(trim($section));
  45. if (!$cablemodemModel) {
  46. $cablemodemModel = new CablemodemModel;
  47. $cablemodemModel->setName($section);
  48. }
  49. preg_match("/.*-(MGCP|SIP).*/", $section, $matches);
  50. if (isset($matches[1])) {
  51. $real_name = preg_replace("|-" . $matches[1] . "|", '', $section);
  52. $technology = strtolower($matches[1]);
  53. $cablemodemModel->setTechnology($technology);
  54. }
  55. $this->output->writeln(sprintf('<info>Model: %s</info>', $cablemodemModel->getName()));
  56. if ($cablemodemModel->getTechnology() && isset($config['terminales'])) {
  57. $cablemodemModel->setTerminals($config['terminales']);
  58. }
  59. $mtaFile = $path . "/" . $section . '.mta.tpl';
  60. $cablemodemModel->setMtaDocsisTemplate($this->getTemplate($mtaFile));
  61. $modelFile = $path . "/" . $section . '.model.tpl';
  62. $cablemodemModel->setDocsisTemplate($this->getTemplate($modelFile));
  63. $this->em->persist($cablemodemModel);
  64. }
  65. }
  66. // template docsis
  67. $this->em->persist($this->getTemplate($path . "/template_docsis.tpl"));
  68. $this->em->flush();
  69. return;
  70. }
  71. /**
  72. * @param string $filename
  73. *
  74. * @return Template
  75. */
  76. private function getTemplate($filename)
  77. {
  78. $template = null;
  79. $templateRepository = $this->em->getRepository('TemplateBundle:Template');
  80. if (file_exists($filename)) {
  81. $this->output->writeln(sprintf('<info> file: %s</info>', $filename));
  82. $template = $templateRepository->findOneByName(basename($filename));
  83. if (!$template) {
  84. $template = new Template;
  85. $template->setName(basename($filename));
  86. $template->setOwner('iksop');
  87. }
  88. $template->setContent(file_get_contents($filename));
  89. }
  90. return $template;
  91. }
  92. }