/** * Parses current .ini snippet files and generates the matching MySQL queries * * @param string $snippetsDir * @param bool $update if false, UPDATE IGNORE statements are generated. Default true, generates UPDATE .. ON DUPLICATE KEY statements * @return array The array containing the generated queries. */ public function loadToQuery($snippetsDir = null, $update = true) { $snippetsDir = $snippetsDir ?: $this->snippetsDir; if (!file_exists($snippetsDir)) { return array(); } $locales = array(); $finder = new Finder(); $inputAdapter = new \Enlight_Config_Adapter_File(array('configDir' => $snippetsDir)); $queryWriter = new QueryWriter(); $finder->files()->in($snippetsDir); foreach ($finder as $file) { $filePath = $file->getRelativePathname(); if (strpos($filePath, '.ini') == strlen($filePath) - 4) { $namespace = substr($filePath, 0, -4); } else { continue; } $namespaceData = new \Enlight_Components_Snippet_Namespace(array('adapter' => $inputAdapter, 'name' => $namespace)); foreach ($namespaceData->read()->toArray() as $index => $values) { if (!array_key_exists($index, $locales)) { $locales[$index] = 'SET @locale_' . $index . ' = (SELECT id FROM s_core_locales WHERE locale = \'' . $index . '\');'; } $queryWriter->setUpdate($update); $queryWriter->write($values, $namespace, '@locale_' . $index, 1); } } $result = $queryWriter->getQueries(); foreach ($locales as $locale) { array_unshift($result, $locale); } return $result; }
/** * Loads all snippets from all files in $snippetsDir * (including subfolders) and removes them from the database. * * @param null $snippetsDir * @param boolean $removeDirty */ public function removeFromDatabase($snippetsDir = null, $removeDirty = false) { $snippetsDir = $snippetsDir ?: $this->kernelRoot . '/snippets/'; if (!file_exists($snippetsDir)) { return; } $localeRepository = $this->em->getRepository('Shopware\\Models\\Shop\\Locale'); $inputAdapter = new \Enlight_Config_Adapter_File(array('configDir' => $snippetsDir)); $outputAdapter = new \Enlight_Config_Adapter_DbTable(array('db' => $this->db, 'table' => 's_core_snippets', 'namespaceColumn' => 'namespace', 'sectionColumn' => array('shopID', 'localeID'))); $finder = new Finder(); $finder->files()->in($snippetsDir); $defaultLocale = $localeRepository->findOneBy(array('locale' => 'en_GB')); foreach ($finder as $file) { $filePath = $file->getRelativePathname(); if (strpos($filePath, '.ini') == strlen($filePath) - 4) { $namespace = substr($filePath, 0, -4); } else { continue; } if ($this->output) { $this->output->writeln('<info>Processing ' . $namespace . ' namespace</info>'); } $namespaceData = new \Enlight_Components_Snippet_Namespace(array('adapter' => $inputAdapter, 'name' => $namespace)); foreach ($namespaceData->read()->toArray() as $index => $values) { if ($index == 'default') { $locale = $defaultLocale; } else { $locale = $localeRepository->findOneBy(array('locale' => $index)); } $namespaceData->setSection(array(1, $locale->getId()))->read(); $namespaceData->setData($values); $outputAdapter->delete($namespaceData, array_keys($values), $removeDirty); if ($this->output) { $this->output->writeln('<info>Deleted ' . count($values) . ' snippets from ' . $locale->getLocale() . '</info>'); } } if ($this->output) { $this->output->writeln('<info></info>'); } } }