protected function exportNode($position, Domain $domain, Node $node, ExportPointer $exportPointer) { $data = $node->toArray(TableMap::TYPE_CAMELNAME); if (!$node->getUrn()) { return; } $path = $node->getUrn(); $exportPointer->pushPath($path); if ($node->getId() === $domain->getStartnodeId()) { $domain->setVirtualColumn('startnodePath', $path); } $nodeData = $this->clearData($data, ['id', 'lft', 'rgt', 'lvl', 'pid', 'urn', 'domainId'], ['type' => 0, 'layout' => 'default', 'visible' => true]); $nodeData['sort'] = $position; $contents = $node->getContents(); $contentsData = []; foreach ($contents as $content) { $contentData = $content->toArray(TableMap::TYPE_CAMELNAME); $jsonDecoded = json_decode($contentData['content'], true); if (JSON_ERROR_NONE === json_last_error()) { $contentData['content'] = $jsonDecoded; } $contentData = $this->clearData($contentData, ['id', 'nodeId'], ['template' => 'JarvesBundle:Default:content.html.twig', 'type' => 'text']); $contentsData[] = $contentData; } $nodeData['contents'] = $contentsData; $exportPointer->addData($nodeData, '.yml'); foreach ($node->getChildren() as $idx => $child) { $this->exportNode($idx, $domain, $child, $exportPointer); } $exportPointer->popPath(); }
protected function import(InputInterface $input, OutputInterface $output) { $basePath = 'app/jarves/'; $openedFiles = []; $domains = []; $nodes = []; $rootNodes = []; $ymlParser = new Parser(); Propel::getWriteConnection('default')->beginTransaction(); //import files $fileReferencesPath = $basePath . '/file_references.yml'; if (file_exists($fileReferencesPath)) { $openedFiles[$fileReferencesPath] = filemtime($fileReferencesPath); $fileReferences = $ymlParser->parse(file_get_contents($fileReferencesPath)); $output->writeln(sprintf('Import %d file references ...', count($fileReferences))); FileQuery::create()->deleteAll(); foreach ($fileReferences as $id => $path) { $file = new File(); $file->setId($id); $file->setPath($path); $file->save(); } } $relativePathBase = $basePath . 'website/'; $dir = opendir($relativePathBase); while ($domainName = readdir($dir)) { if ('.' === $domainName || '..' === $domainName || '.yml' === substr($domainName, -4)) { continue; } $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($basePath . 'website/' . $domainName)); $files = iterator_to_array($files); ksort($files); $output->writeln(sprintf('Import domain %s ...', $domainName)); $domain = DomainQuery::create()->findOneByDomain($domainName); if (!$domain) { $domain = new Domain(); } $ymlPath = $relativePathBase . $domainName . '.yml'; $domainFromYml = $ymlParser->parse(file_get_contents($ymlPath)); $openedFiles[$ymlPath] = filemtime($ymlPath); $oldData = $domain->toArray(TableMap::TYPE_CAMELNAME); $domain->fromArray(array_merge($oldData, $domainFromYml), TableMap::TYPE_CAMELNAME); $domain->setStartnodeId(null); if (isset($domainFromYml['startnode'])) { $domain->setVirtualColumn('startnodePath', $domainFromYml['startnode']); } $domain->save(); $domains[$domainName] = $domain; NodeQuery::create()->filterByDomain($domain)->delete(); $rootNode = new Node(); $rootNode->setTitle('root'); $rootNode->makeRoot(); $rootNode->setDomain($domain); $rootNode->save(); $rootNodes[$domainName] = $rootNode; $nodes[''] = $rootNode; $parentNodeQueue = []; /** @var \SplFileInfo $file */ foreach ($files as $file) { if ('.' === $file->getFilename() || '..' === $file->getFilename() || '.yml' !== substr($file->getFilename(), -4)) { continue; } $path = $file->getPath() . '/' . $file->getFilename(); $path = substr($path, strlen($relativePathBase . $domainName) + 1); if (!$path) { continue; } $parentPath = ''; if (false !== strpos($path, '/')) { $parentPath = dirname($path); } $baseName = substr(basename($path), 0, -4); //without .yml if ($baseName) { //its a node $node = isset($nodes[$path]) ? $nodes[$path] : new Node(); $ymlPath = $relativePathBase . $domainName . '/' . $path; $nodeFromYml = $ymlParser->parse(file_get_contents($ymlPath)); $openedFiles[$ymlPath] = filemtime($ymlPath); $node->fromArray($nodeFromYml, TableMap::TYPE_CAMELNAME); $node->setDomain($domain); $urn = $baseName; if (false !== ($dotPos = strpos($baseName, '.'))) { $prefix = substr($baseName, 0, $dotPos); if ($prefix) { $urn = substr($baseName, $dotPos + 1); } } $node->setUrn($urn); $output->writeln(sprintf('Import page %s%s ...', str_repeat(' ', substr_count($path, '/')), $node->getTitle())); $nodes[substr($path, 0, -4)] = $node; $end = isset($parentNodeQueue[$parentPath]) ? count($parentNodeQueue[$parentPath]) : 1; $position = isset($nodeFromYml['sort']) ? $nodeFromYml['sort'] : $end; $parentNodeQueue[$parentPath][$position][] = $node; if (isset($nodeFromYml['contents'])) { foreach ($nodeFromYml['contents'] as $idx => $contentFromYaml) { if (isset($contentFromYaml['content']) && is_array($contentFromYaml['content'])) { $contentFromYaml['content'] = json_encode($contentFromYaml['content']); } $content = new Content(); $content->setSort($idx); $content->fromArray($contentFromYaml, TableMap::TYPE_CAMELNAME); $content->setNode($node); } if ($domain->hasVirtualColumn('startnodePath') && substr($path, 0, -4) === $domain->getVirtualColumn('startnodePath')) { $domain->setStartnode($node); } } } } //save queued nodes foreach ($parentNodeQueue as $parentPath => $nodesQueue) { ksort($nodesQueue); //key is sort /** @var Node $node */ foreach ($nodesQueue as $position => $nodesToInsert) { foreach ($nodesToInsert as $node) { $node->insertAsLastChildOf($nodes[$parentPath]); $node->save(); //saves Content as well. } } } $domain->save(); } Propel::getWriteConnection('default')->commit(); $cacher = $this->getContainer()->get('jarves.cache.cacher'); $cacher->invalidateCache('core'); return $openedFiles; }