123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace CablemodemBundle\Command;
- use CablemodemBundle\Entity\CablemodemModel;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Finder\Finder;
- use TemplateBundle\Entity\Template;
- class InitDocsisTemplatesCommand extends ContainerAwareCommand
- {
- /**
- * @see Command
- */
- protected function configure()
- {
- $this
- ->setName('init:templates:docsis')
- ->setDescription('Init Docsis FD3 Templates For the CableModem Models')
- ->setDefinition(array(
- new InputArgument('path', InputArgument::OPTIONAL, "Directory of templates", realpath(__DIR__.'/../Resources/docsis-templates/')),
- ))
- ->setHelp(<<<EOT
- The <info>init:templates:docsis</info> command import all the template docsis configuration, including cablemodem models and their corresponding templates
- EOT
- );
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $path = $input->getArgument('path');
- $this->output = $output;
- $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
- $finder = new Finder();
- $finder->files()->name("*.ini")->in($path);
- foreach ($finder as $file) {
- $iniFile = $file->getRealpath();
- $config = parse_ini_file($iniFile, true);
- $this->output->writeln("Open Ini File <info>$iniFile</info>");
- foreach ($config as $section => $config) {
- $cablemodemModel = $this->em->getRepository('CablemodemBundle:CablemodemModel')->findOneByName(trim($section));
- if (!$cablemodemModel) {
- $cablemodemModel = new CablemodemModel;
- $cablemodemModel->setName($section);
- }
- preg_match("/.*-(MGCP|SIP).*/", $section, $matches);
- if (isset($matches[1])) {
- $real_name = preg_replace("|-" . $matches[1] . "|", '', $section);
- $technology = strtolower($matches[1]);
- $cablemodemModel->setTechnology($technology);
- }
- $this->output->writeln(sprintf('<info>Model: %s</info>', $cablemodemModel->getName()));
- if ($cablemodemModel->getTechnology() && isset($config['terminales'])) {
- $cablemodemModel->setTerminals($config['terminales']);
- }
- $mtaFile = $path . "/" . $section . '.mta.tpl';
- $cablemodemModel->setMtaDocsisTemplate($this->getTemplate($mtaFile));
- $modelFile = $path . "/" . $section . '.model.tpl';
- $cablemodemModel->setDocsisTemplate($this->getTemplate($modelFile));
- $this->em->persist($cablemodemModel);
- }
- }
-
- // template docsis
- $this->em->persist($this->getTemplate($path . "/template_docsis.tpl"));
- $this->em->flush();
- return;
- }
- /**
- * @param string $filename
- *
- * @return Template
- */
- private function getTemplate($filename)
- {
- $template = null;
- $templateRepository = $this->em->getRepository('TemplateBundle:Template');
- if (file_exists($filename)) {
- $this->output->writeln(sprintf('<info> file: %s</info>', $filename));
- $template = $templateRepository->findOneByName(basename($filename));
- if (!$template) {
- $template = new Template;
- $template->setName(basename($filename));
- $template->setOwner('iksop');
- }
- $template->setContent(file_get_contents($filename));
- }
- return $template;
- }
- }
|