/** * @Route("/change_status/{id}") * @ParamConverter("get") */ public function changeStatusAction(Request $request, Parser $parser) { $status = true; $message = $message = $this->get('translator')->trans('plugin.ingest.parsers.activationsuccess', array('%parser%' => $parser->getName())); $em = $this->get('em'); try { $active = $parser->getActive(); $parser->setActive(!$active); $em->persist($parser); $em->flush(); } catch (\Exception $e) { $status = false; $message = $this->get('translator')->trans('plugin.ingest.parsers.activationfailed', array('%parser%' => $parser->getName(), '%error%' => $e->getMessage())); } return new JsonResponse(array('status' => $status, 'message' => $message)); }
/** * Install external parsers * * @return void * * @throws Exception */ private function installParsers(IngestParsersEvent $event) { $fs = new Filesystem(); $finder = new Finder(); try { $pluginsDir = __DIR__ . '/../../../'; $namespaces = array(); $iterator = $finder->ignoreUnreadableDirs()->files()->name('*Parser.php')->notName('AbstractParser.php')->in(__DIR__ . '/../Parsers/'); try { $iterator = $iterator->in($pluginsDir . '*/*/Parsers/IngestAdapters/'); } catch (\Exception $e) { // Catch exception if no such directory exists } foreach ($iterator as $file) { $classNamespace = str_replace(realpath($pluginsDir), '', substr($file->getRealPath(), 0, -4)); $namespace = str_replace('/', '\\', $classNamespace); $namespaces[] = (string) $namespace; $parserName = substr($file->getFilename(), 0, -4); $parser = $this->em->getRepository('Newscoop\\IngestPluginBundle\\Entity\\Parser')->findOneByNamespace($namespace); $event->registerParser($parserName, array('class' => $namespace)); if (!$parser) { $parser = new Parser(); } $parser->setName($namespace::getParserName())->setDescription($namespace::getParserDescription())->setDomain($namespace::getParserDomain())->setRequiresUrl($namespace::getRequiresUrl())->setSectionHandling($namespace::getHandlesSection())->setNamespace($namespace); $this->em->persist($parser); } // Remove parser which we didn't find anymore $parsersToRemove = $this->em->createQuery(' DELETE FROM Newscoop\\IngestPluginBundle\\Entity\\Parser AS p WHERE p.namespace NOT IN (:namespaces) ')->setParameter('namespaces', $namespaces)->getResult(); $this->em->flush(); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } }