-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add routable-interface and the handling of routes in the template data (
#43) * add routable-interface * fix review comments * add trest for routable trait
- Loading branch information
1 parent
756944d
commit 75621b4
Showing
35 changed files
with
3,042 additions
and
7 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
Content/Application/ContentDimensionFactory/Mapper/RouteMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDimensionFactory\Mapper; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; | ||
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface; | ||
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface; | ||
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; | ||
use Sulu\Component\Content\Metadata\PropertyMetadata; | ||
use Sulu\Component\Content\Metadata\StructureMetadata; | ||
|
||
class RouteMapper implements MapperInterface | ||
{ | ||
/** | ||
* @var StructureMetadataFactoryInterface | ||
*/ | ||
private $factory; | ||
|
||
/** | ||
* @var RouteGeneratorInterface | ||
*/ | ||
private $routeGenerator; | ||
|
||
/** | ||
* @var RouteManagerInterface | ||
*/ | ||
private $routeManager; | ||
|
||
public function __construct( | ||
StructureMetadataFactoryInterface $factory, | ||
RouteGeneratorInterface $routeGenerator, | ||
RouteManagerInterface $routeManager | ||
) { | ||
$this->factory = $factory; | ||
$this->routeGenerator = $routeGenerator; | ||
$this->routeManager = $routeManager; | ||
} | ||
|
||
public function map( | ||
array $data, | ||
object $contentDimension, | ||
?object $localizedContentDimension = null | ||
): void { | ||
if (!$localizedContentDimension || !$localizedContentDimension instanceof RoutableInterface) { | ||
return; | ||
} | ||
|
||
if (!$localizedContentDimension instanceof TemplateInterface) { | ||
throw new \RuntimeException('ContentDimension needs to extend the TemplateInterface'); | ||
} | ||
|
||
if (!isset($data['template'])) { | ||
throw new \RuntimeException('Expected "template" to be set in the data array.'); | ||
} | ||
|
||
$template = $data['template']; | ||
$type = $localizedContentDimension->getTemplateType(); | ||
|
||
$metadata = $this->factory->getStructureMetadata($type, $template); | ||
if (!$metadata) { | ||
return; | ||
} | ||
|
||
$property = $this->getRouteProperty($metadata); | ||
if (!$property) { | ||
return; | ||
} | ||
|
||
if (!$localizedContentDimension->getContentId()) { | ||
// FIXME the code only works if the content-dimension is flushed once and has a valid id | ||
|
||
return; | ||
} | ||
|
||
$locale = $localizedContentDimension->getLocale(); | ||
if (!$locale) { | ||
return; | ||
} | ||
|
||
/** @var string $name */ | ||
$name = $property->getName(); | ||
if (!array_key_exists($name, $data)) { | ||
return; | ||
} | ||
|
||
$routePath = $data[$name] ?? null; | ||
if (!$routePath) { | ||
// FIXME this should be handled directly in the form - see pages as an example | ||
$routePath = $this->routeGenerator->generate( | ||
$localizedContentDimension, | ||
['route_schema' => '/{object.getTitle()}'] | ||
); | ||
|
||
$localizedContentDimension->setTemplateData( | ||
array_merge( | ||
$localizedContentDimension->getTemplateData(), | ||
[$name => $routePath] | ||
) | ||
); | ||
} | ||
|
||
$this->routeManager->createOrUpdateByAttributes( | ||
$localizedContentDimension->getContentClass(), | ||
(string) $localizedContentDimension->getContentId(), | ||
$locale, | ||
$routePath | ||
); | ||
} | ||
|
||
private function getRouteProperty(StructureMetadata $metadata): ?PropertyMetadata | ||
{ | ||
foreach ($metadata->getProperties() as $property) { | ||
if ('route' === $property->getType()) { | ||
return $property; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Application\Message; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentInterface; | ||
|
||
class LoadContentViewMessage | ||
{ | ||
/** | ||
* @var ContentInterface | ||
*/ | ||
private $content; | ||
|
||
/** | ||
* @var mixed[] | ||
*/ | ||
private $dimensionAttributes; | ||
|
||
/** | ||
* @param mixed[] $dimensionAttributes | ||
*/ | ||
public function __construct(ContentInterface $content, array $dimensionAttributes) | ||
{ | ||
$this->content = $content; | ||
$this->dimensionAttributes = $dimensionAttributes; | ||
} | ||
|
||
public function getContent(): ContentInterface | ||
{ | ||
return $this->content; | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function getDimensionAttributes(): array | ||
{ | ||
return $this->dimensionAttributes; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Content/Application/MessageHandler/LoadContentViewMessageHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Application\MessageHandler; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Application\ContentDimensionLoader\ContentDimensionLoaderInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Application\Message\LoadContentViewMessage; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ViewFactoryInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentViewInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionRepositoryInterface; | ||
|
||
class LoadContentViewMessageHandler | ||
{ | ||
/** | ||
* @var DimensionRepositoryInterface | ||
*/ | ||
private $dimensionRepository; | ||
|
||
/** | ||
* @var ContentDimensionLoaderInterface | ||
*/ | ||
private $contentDimensionLoader; | ||
|
||
/** | ||
* @var ViewFactoryInterface | ||
*/ | ||
private $viewFactory; | ||
|
||
public function __construct( | ||
DimensionRepositoryInterface $dimensionRepository, | ||
ContentDimensionLoaderInterface $contentDimensionLoader, | ||
ViewFactoryInterface $viewFactory | ||
) { | ||
$this->dimensionRepository = $dimensionRepository; | ||
$this->contentDimensionLoader = $contentDimensionLoader; | ||
$this->viewFactory = $viewFactory; | ||
} | ||
|
||
public function __invoke(LoadContentViewMessage $message): ContentViewInterface | ||
{ | ||
$content = $message->getContent(); | ||
$dimensionCollection = $this->dimensionRepository->findByAttributes($message->getDimensionAttributes()); | ||
$contentDimensionCollection = $this->contentDimensionLoader->load($content, $dimensionCollection); | ||
|
||
return $this->viewFactory->create($contentDimensionCollection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Domain\Model; | ||
|
||
/** | ||
* Marker interface for autoloading the doctrine metadata for routables. | ||
*/ | ||
interface RoutableInterface | ||
{ | ||
public function getContentClass(): string; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getContentId(); | ||
|
||
public function getLocale(): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Domain\Model; | ||
|
||
trait RoutableTrait | ||
{ | ||
public function getLocale(): ?string | ||
{ | ||
return $this->getDimension()->getLocale(); | ||
} | ||
|
||
abstract public function getDimension(): DimensionInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.