Skip to content

Instantly share code, notes, and snippets.

@bdunogier
Last active October 11, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdunogier/e55ccc1eafda264dc5abb8ee1de3d394 to your computer and use it in GitHub Desktop.
Save bdunogier/e55ccc1eafda264dc5abb8ee1de3d394 to your computer and use it in GitHub Desktop.
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace BD\SandboxBundle\Command;
use eZ\Publish\API\Repository\Values\Content\Content;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class IssueEzp26367Command extends ContainerAwareCommand
{
/**
* @var \eZ\Publish\API\Repository\Values\Content\Content
*/
private $folder;
/**
* @var \eZ\Publish\API\Repository\Values\Content\Content
*/
private $article;
/**
* @var \eZ\Publish\API\Repository\Values\Content\URLAlias
*/
private $alias;
/**
* @var String
*/
private $folderAliasPath;
/**
* @var String
*/
private $articleAliasPath;
protected function configure()
{
$this->setName('bd:issue:ezp-26367');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loginAsAdmin();
$this->createFolder();
$this->createArticleWithinFolder();
$this->dumpArticleAliases();
$this->renameFolder();
$this->lookupArticleAliasPath();
$this->dumpArticleAliases();
$this->dumpArticleAliases();
}
private function loginAsAdmin()
{
$repository = $this->getContainer()->get('ezpublish.api.repository');
$userService = $this->getContainer()->get('ezpublish.api.service.user');
$repository->setCurrentUser($userService->loadUserByLogin('admin'));
}
/**
* @return Content
*/
private function createFolder()
{
$repository = $this->getContainer()->get('ezpublish.api.repository');
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$struct = $contentService->newContentCreateStruct(
$repository->getContentTypeService()->loadContentTypeByIdentifier('folder'),
'eng-GB'
);
$struct->setField('name', 'EZP-26367 ' . uniqid());
$folder = $contentService->createContent(
$struct,
[$locationService->newLocationCreateStruct(2)]
);
$this->folder = $contentService->publishVersion($folder->versionInfo);
$this->folderAliasPath = $this->getContainer()->get('ezpublish.persistence.slug_converter')->convert($folder->contentInfo->name);
$this->notice("Created folder " . $folder->contentInfo->name);
}
private function createArticleWithinFolder()
{
$repository = $this->getContainer()->get('ezpublish.api.repository');
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$struct = $contentService->newContentCreateStruct(
$repository->getContentTypeService()->loadContentTypeByIdentifier('article'),
'eng-GB'
);
$struct->setField('title', 'Article');
$struct->setField('intro', '<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit"><p>Some content</p></section>');
$content = $contentService->createContent(
$struct,
[$locationService->newLocationCreateStruct($this->folder->contentInfo->mainLocationId)]
);
$this->article = $contentService->publishVersion($content->versionInfo);
$this->articleAliasPath = $this->folderAliasPath . '/' . 'Article';
$this->notice("Created article under " . $this->folder->contentInfo->name);
}
private function renameFolder()
{
$repository = $this->getContainer()->get('ezpublish.api.repository');
$contentService = $repository->getContentService();
$draftContent = $contentService->createContentDraft($this->folder->contentInfo);
$struct = $contentService->newContentUpdateStruct();
$oldName = $this->folder->getFieldValue('name');
$newName = $oldName . ' renamed';
$struct->setField('name', $newName);
$content = $contentService->updateContent(
$draftContent->versionInfo,
$struct
);
$this->folder = $contentService->publishVersion($content->versionInfo);
$this->notice("Renamed folder from '$oldName' to '$newName'");
}
private function dumpArticleAliases()
{
$this->notice("Dumping UrlAlias for article");
$urlAliasService = $this->getContainer()->get('ezpublish.api.service.url_alias');
$locationService = $this->getContainer()->get('ezpublish.api.service.location');
$this->alias = $urlAliasService->listLocationAliases(
$locationService->loadLocation($this->article->contentInfo->mainLocationId),
false,
null,
null,
['eng-GB']
)[0];
dump($this->alias);
}
private function lookupDumpedAlias()
{
$this->notice('Looking up for dumped alias path');
dump(
$this->getContainer()->get('ezpublish.api.service.url_alias')->lookup(
$this->alias->path
)
);
}
/**
* @return \Psr\Log\LoggerInterface
*/
private function getLogger()
{
return $this->getContainer()->get('logger');
}
private function lookupArticleAliasPath()
{
$this->notice("Looking up article alias path '$this->articleAliasPath'");
dump(
$this->getUrlAliasService()->lookup($this->articleAliasPath)
);
}
/**
* @return \eZ\Publish\Core\Repository\URLAliasService
*/
private function getUrlAliasService()
{
return $this->getContainer()->get('ezpublish.api.service.url_alias');
}
private function notice($message)
{
$this->getLogger()->notice($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment