Neos\Neos\Domain\Service\SiteImportService::importFromFile PHP Method

importFromFile() public method

Imports one or multiple sites from the XML file at $pathAndFilename
public importFromFile ( string $pathAndFilename ) : Site
$pathAndFilename string
return Neos\Neos\Domain\Model\Site The imported site
    public function importFromFile($pathAndFilename)
    {
        /** @var Site $importedSite */
        $site = null;
        $xmlReader = new \XMLReader();
        $xmlReader->open($pathAndFilename, null, LIBXML_PARSEHUGE);
        if ($this->workspaceRepository->findOneByName('live') === null) {
            $this->workspaceRepository->add(new Workspace('live'));
            $this->persistenceManager->persistAll();
        }
        while ($xmlReader->read()) {
            if ($xmlReader->nodeType != \XMLReader::ELEMENT || $xmlReader->name !== 'site') {
                continue;
            }
            $site = $this->getSiteByNodeName($xmlReader->getAttribute('siteNodeName'));
            $site->setName($xmlReader->getAttribute('name'));
            $site->setState((int) $xmlReader->getAttribute('state'));
            $siteResourcesPackageKey = $xmlReader->getAttribute('siteResourcesPackageKey');
            if (!$this->packageManager->isPackageAvailable($siteResourcesPackageKey)) {
                throw new UnknownPackageException(sprintf('Package "%s" specified in the XML as site resources package does not exist.', $siteResourcesPackageKey), 1303891443);
            }
            if (!$this->packageManager->isPackageActive($siteResourcesPackageKey)) {
                throw new InvalidPackageStateException(sprintf('Package "%s" specified in the XML as site resources package is not active.', $siteResourcesPackageKey), 1303898135);
            }
            $site->setSiteResourcesPackageKey($siteResourcesPackageKey);
            $rootNode = $this->contextFactory->create()->getRootNode();
            // We fetch the workspace to be sure it's known to the persistence manager and persist all
            // so the workspace and site node are persisted before we import any nodes to it.
            $rootNode->getContext()->getWorkspace();
            $this->persistenceManager->persistAll();
            $sitesNode = $rootNode->getNode(SiteService::SITES_ROOT_PATH);
            if ($sitesNode === null) {
                $sitesNode = $rootNode->createNode(NodePaths::getNodeNameFromPath(SiteService::SITES_ROOT_PATH));
            }
            $this->nodeImportService->import($xmlReader, $sitesNode->getPath(), dirname($pathAndFilename) . '/Resources');
        }
        if ($site === null) {
            throw new NeosException(sprintf('The XML file did not contain a valid site node.'), 1418999522);
        }
        $this->emitSiteImported($site);
        return $site;
    }

Usage Example

 public function setUp()
 {
     parent::setUp();
     $this->markSkippedIfNodeTypesPackageIsNotInstalled();
     $this->contextFactory = $this->objectManager->get(ContextFactoryInterface::class);
     $contentContext = $this->contextFactory->create(array('workspaceName' => 'live'));
     $this->siteImportService = $this->objectManager->get(SiteImportService::class);
     $this->siteExportService = $this->objectManager->get(SiteExportService::class);
     $this->importedSite = $this->siteImportService->importFromFile(__DIR__ . '/' . $this->fixtureFileName, $contentContext);
     $this->persistenceManager->persistAll();
 }
All Usage Examples Of Neos\Neos\Domain\Service\SiteImportService::importFromFile