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(<<init:templates:docsis 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 $iniFile"); 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('Model: %s', $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(' file: %s', $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; } }