Skip to content

Commit

Permalink
implemented by-type and by-template route-generator (#14)
Browse files Browse the repository at this point in the history
* immplemented by-type and by-template route-generator

* fixed testcases to fit new sulu

* added tests for custom-route-generators

* changed config from service-id to alias

* changed alias of route_generators

* changed alias of route_generators
  • Loading branch information
wachterjohannes authored and chirimoya committed Aug 18, 2016
1 parent 6c9938f commit cfebfb9
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Admin/ArticleJsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
namespace Sulu\Bundle\ArticleBundle\Admin;

use Sulu\Bundle\AdminBundle\Admin\JsConfigInterface;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;

/**
* Provides js-configuration.
*/
class ArticleJsConfig implements JsConfigInterface
{
use TypeTrait;
use ArticleTypeTrait;

/**
* @var StructureManagerInterface
Expand Down
4 changes: 2 additions & 2 deletions Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Sulu\Bundle\ArticleBundle\Controller;

use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -21,7 +21,7 @@
*/
class TemplateController extends Controller
{
use TypeTrait;
use ArticleTypeTrait;

/**
* Returns template for given article type.
Expand Down
4 changes: 2 additions & 2 deletions Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Sulu\Bundle\ArticleBundle\Document\ExcerptOngrObject;
use Sulu\Bundle\ArticleBundle\Document\MediaCollectionOngrObject;
use Sulu\Bundle\ArticleBundle\Document\SeoOngrObject;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Bundle\SecurityBundle\UserManager\UserManager;
use Sulu\Bundle\TagBundle\Tag\TagManagerInterface;
use Sulu\Component\Content\Compat\StructureManagerInterface;
Expand All @@ -27,7 +27,7 @@
*/
class ArticleIndexer implements IndexerInterface
{
use TypeTrait;
use ArticleTypeTrait;

/**
* @var StructureManagerInterface
Expand Down
4 changes: 2 additions & 2 deletions Document/Serializer/TypeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Util\TypeTrait;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Component\Content\Compat\StructureManagerInterface;

/**
* Appends type to article-document.
*/
class TypeSubscriber implements EventSubscriberInterface
{
use TypeTrait;
use ArticleTypeTrait;

/**
* @var StructureManagerInterface
Expand Down
62 changes: 62 additions & 0 deletions Exception/RouteSchemaNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Exception;

/**
* Will be thrown when the requested schema is not configured.
*/
class RouteSchemaNotFoundException extends \Exception
{
/**
* @var string
*/
private $requested;

/**
* @var string[]
*/
private $available;

/**
* @param string $requested
* @param string[] $available
*/
public function __construct($requested, array $available)
{
parent::__construct(
sprintf('Route-schema for "%s" not configured. Available: ["%s"]', $requested, implode('","', $available))
);

$this->requested = $requested;
$this->available = $available;
}

/**
* Returns requested.
*
* @return string
*/
public function getRequested()
{
return $this->requested;
}

/**
* Returns requested.
*
* @return string[]
*/
public function getAvailable()
{
return $this->available;
}
}
4 changes: 2 additions & 2 deletions Util/TypeTrait.php → Metadata/ArticleTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Util;
namespace Sulu\Bundle\ArticleBundle\Metadata;

use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin;
use Sulu\Component\Content\Metadata\StructureMetadata;

/**
* Encapsulates function to extract the type from structure-metadata.
*/
trait TypeTrait
trait ArticleTypeTrait
{
/**
* Returns type for given structure-metadata.
Expand Down
15 changes: 15 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@
<tag name="sulu_route.defaults_provider"/>
</service>

<!-- route-generator -->
<service id="sulu_article.route_generator.by_type"
class="Sulu\Bundle\ArticleBundle\Routing\ArticleRouteGeneratorByType">
<argument type="service" id="sulu_route.generator.route_generator"/>
<argument type="service" id="sulu_content.structure.factory"/>

<tag name="sulu.route_generator" alias="type"/>
</service>
<service id="sulu_article.route_generator.by_template"
class="Sulu\Bundle\ArticleBundle\Routing\ArticleRouteGeneratorByTemplate">
<argument type="service" id="sulu_route.generator.route_generator"/>

<tag name="sulu.route_generator" alias="template"/>
</service>

<!-- content -->
<service id="sulu_article.content.data_provider.proxy_factory"
class="ProxyManager\Factory\LazyLoadingValueHolderFactory"/>
Expand Down
60 changes: 60 additions & 0 deletions Routing/ArticleRouteGeneratorByTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Routing;

use Sulu\Bundle\ArticleBundle\Exception\RouteSchemaNotFoundException;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Generate route for articles by type.
*/
class ArticleRouteGeneratorByTemplate implements RouteGeneratorInterface
{
use ArticleTypeTrait;

/**
* @var RouteGeneratorInterface
*/
private $routeGenerator;

/**
* {@inheritdoc}
*/
public function __construct(RouteGeneratorInterface $routeGenerator)
{
$this->routeGenerator = $routeGenerator;
}

/**
* {@inheritdoc}
*/
public function generate($entity, array $options)
{
$template = $entity->getStructureType();

if (!array_key_exists($template, $options)) {
throw new RouteSchemaNotFoundException($template, array_keys($options));
}

return $this->routeGenerator->generate($entity, ['route_schema' => $options[$template]]);
}

/**
* {@inheritdoc}
*/
public function getOptionsResolver(array $options)
{
return (new OptionsResolver())->setDefined(array_keys($options));
}
}
71 changes: 71 additions & 0 deletions Routing/ArticleRouteGeneratorByType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Routing;

use Sulu\Bundle\ArticleBundle\Exception\RouteSchemaNotFoundException;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Generate route for articles by type.
*/
class ArticleRouteGeneratorByType implements RouteGeneratorInterface
{
use ArticleTypeTrait;

/**
* @var RouteGeneratorInterface
*/
private $routeGenerator;

/**
* @var StructureMetadataFactoryInterface
*/
private $structureMetadataFactory;

/**
* {@inheritdoc}
*/
public function __construct(
RouteGeneratorInterface $routeGenerator,
StructureMetadataFactoryInterface $structureMetadataFactory
) {
$this->routeGenerator = $routeGenerator;
$this->structureMetadataFactory = $structureMetadataFactory;
}

/**
* {@inheritdoc}
*/
public function generate($entity, array $options)
{
$type = $this->getType(
$this->structureMetadataFactory->getStructureMetadata('article', $entity->getStructureType())
);

if (!array_key_exists($type, $options)) {
throw new RouteSchemaNotFoundException($type, array_keys($options));
}

return $this->routeGenerator->generate($entity, ['route_schema' => $options[$type]]);
}

/**
* {@inheritdoc}
*/
public function getOptionsResolver(array $options)
{
return (new OptionsResolver())->setDefined(array_keys($options));
}
}
1 change: 0 additions & 1 deletion Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function testGet()
$this->assertHttpStatusCode(200, $client->getResponse());

$response = json_decode($client->getResponse()->getContent(), true);

foreach ($article as $name => $value) {
$this->assertEquals($value, $response[$name]);
}
Expand Down
59 changes: 59 additions & 0 deletions Tests/Unit/Routing/ArticleRouteGeneratorByTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Tests\Unit\Routing;

use Sulu\Bundle\ArticleBundle\Exception\RouteSchemaNotFoundException;
use Sulu\Bundle\ArticleBundle\Routing\ArticleRouteGeneratorByTemplate;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface;
use Sulu\Component\Content\Document\Behavior\StructureBehavior;

class ArticleRouteGeneratorByTemplateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RouteGeneratorInterface
*/
private $generatorBySchema;

/**
* @var RouteGeneratorInterface
*/
private $generatorByTemplate;

private $config = ['test1' => '/test1/{entity.getTitle()', 'test2' => '/test2/{entity.getTitle()'];

public function setUp()
{
$this->generatorBySchema = $this->prophesize(RouteGeneratorInterface::class);
$this->generatorByTemplate = new ArticleRouteGeneratorByTemplate($this->generatorBySchema->reveal());
}

public function testGenerate()
{
$entity = $this->prophesize(StructureBehavior::class);
$entity->getStructureType()->willReturn('test1');

$this->generatorByTemplate->generate($entity->reveal(), $this->config);

$this->generatorBySchema->generate($entity->reveal(), ['route_schema' => '/test1/{entity.getTitle()'])
->shouldBeCalled();
}

public function testGenerateNotConfigured()
{
$this->setExpectedException(RouteSchemaNotFoundException::class);

$entity = $this->prophesize(StructureBehavior::class);
$entity->getStructureType()->willReturn('test3');

$this->generatorByTemplate->generate($entity->reveal(), $this->config);
}
}
Loading

0 comments on commit cfebfb9

Please sign in to comment.