Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Added Sylius Resource Enhancer
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed May 29, 2016
1 parent 46b3669 commit 4df78ef
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 17 deletions.
45 changes: 33 additions & 12 deletions Description/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,64 @@ public function __construct(PuliResource $resource)
}

/**
* Return the descriptors value for the given key.
* Return the descriptors value for the given descriptor.
*
* @param string $key
* @param string $descriptor
*
* @return mixed
*/
public function get($key)
public function get($descriptor)
{
if (!isset($this->descriptors[$key])) {
if (!isset($this->descriptors[$descriptor])) {
throw new \InvalidArgumentException(sprintf(
'Descriptor "%s" not supported for resource "%s" of class "%s". Supported descriptors: "%s"',
$key,
$descriptor,
$this->resource->getPath(),
get_class($this->resource),
implode('", "', array_keys($this->descriptors))
));
}

return $this->descriptors[$key];
return $this->descriptors[$descriptor];
}

/**
* Set value for descriptors key.
* Return true if the given descriptor has been set.
*
* @param string $descriptor
* @return bool
*/
public function has($descriptor)
{
return isset($this->descriptors[$descriptor]);
}

/**
* Return all of the descriptors.
*
* @return array
*/
public function all()
{
return $this->descriptors;
}

/**
* Set value for descriptors descriptor.
*
* Note that:
*
* - It is possible to overwrite existing keys.
* - It is possible to overwrite existing descriptors.
*
* - Where possible the key should be the value of one of the constants
* - Where possible the descriptor should be the value of one of the constants
* defined in the Descriptor class.
*
* @param string $key
* @param string $descriptor
* @param mixed $value
*/
public function set($key, $value)
public function set($descriptor, $value)
{
$this->descriptors[$key] = $value;
$this->descriptors[$descriptor] = $value;
}

/**
Expand Down
64 changes: 61 additions & 3 deletions Description/Enhancer/Sylius/ResourceEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

use Sylius\Component\Resource\Metadata\RegistryInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
use Symfony\Cmf\Component\Resource\Description\Description;
use Puli\Repository\Api\Resource\PuliResource;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactory;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Cmf\Component\Resource\Description\Descriptor;

/**
* Add descriptors from the Sylius Resource component.
Expand All @@ -31,18 +38,58 @@ class ResourceEnhancer implements DescriptionEnhancerInterface
*/
private $registry;

public function __construct(RegistryInterface $registry, UrlGeneratorInterface $urlGenerator)
/**
* @var RequestConfigurationFactory
*/
private $requestConfigurationFactory;

/**
* @var RequestStack
*/
private $requestStack;

public function __construct(
RegistryInterface $registry,
RequestStack $requestStack,
RequestConfigurationFactory $requestConfigurationFactory,
UrlGeneratorInterface $urlGenerator
)
{
$this->registry = $registry;
$this->urlGenerator = $urlGenerator;
$this->requestConfigurationFactory = $requestConfigurationFactory;
$this->requestStack = $requestStack;
}

/**
* {@inheritdoc}
*/
public function enhance(Description $description)
{
$object = $description->getResource()->getPayload();
$metadata = $this->registry->getByClass($description->getResource()->getPayloadType());
$payload = $description->getResource()->getPayload();

// the request configuration provides the route names.
$request = $this->requestStack->getCurrentRequest();
$configuration = $this->requestConfigurationFactory->create($metadata, $request);

$map = [
Descriptor::LINK_SHOW_HTML => 'show',
Descriptor::LINK_LIST_HTML => 'index',
Descriptor::LINK_EDIT_HTML => 'update',
Descriptor::LINK_CREATE_HTML => 'create',
Descriptor::LINK_REMOVE_HTML=> 'delete',
];

foreach ($map as $descriptor => $action) {
$url = $this->urlGenerator->generate(
$configuration->getRouteName($action),
[
'id' => $payload->getId()
]
);
$description->set($descriptor, $url);
}
}

/**
Expand All @@ -54,6 +101,17 @@ public function supports(PuliResource $resource)
return false;
}

throw new \Exception('here');
try {
$metadata = $this->registry->getByClass($resource->getPayloadType());
} catch (\InvalidArgumentException $e) {
return false;
}

return true;
}

private function getRouteName(Metadata $metadata, $action)
{
return sprintf('%s_%s_%s', $metadata->getApplicationName(), $metadata->getName(), $action);
}
}
9 changes: 9 additions & 0 deletions Tests/Unit/Description/DescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function testGetSet()
$this->description->set('custom.key', 'Hello');

$this->assertEquals('page', $this->description->get(Descriptor::TYPE_ALIAS));

$this->assertTrue($this->description->has(Descriptor::TYPE_ALIAS));
$this->assertFalse($this->description->has('hello'));
$this->assertEquals([
Descriptor::TYPE_ALIAS => 'page',
Descriptor::LINK_EDIT_HTML => '/path/to/edit',
'custom.key' => 'Hello',

], $this->description->all());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Tests/Unit/Description/Enhancer/Sonata/AdminEnhancerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Prophecy\Argument;
use Symfony\Cmf\Component\Resource\Description\Descriptor;
use Symfony\Cmf\Component\Resource\Description\Enhancer\Sonata\AdminEnhancer;

class SonataAdminEnhancerTest extends \PHPUnit_Framework_TestCAse
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public function testDescriptionProvide()
});

$description = new Description($this->resource->reveal());
$enhancer = new SonataAdminEnhancer($this->pool, $this->generator->reveal());
$enhancer = new AdminEnhancer($this->pool, $this->generator->reveal());
$enhancer->enhance($description);

$this->assertEquals('/std_class_edit', $description->get(Descriptor::LINK_EDIT_HTML));
Expand Down
104 changes: 104 additions & 0 deletions Tests/Unit/Description/Enhancer/Sylius/ResourceEnhancerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,110 @@

namespace Symfony\Cmf\Component\Resource\Tests\Unit\Description\Enhancer;

use Sylius\Component\Resource\Metadata\RegistryInterface;
use Symfony\Cmf\Component\Resource\Description\Enhancer\Sylius\ResourceEnhancer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Sylius\Component\Resource\Metadata\Metadata;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactory;
use Puli\Repository\Api\Resource\PuliResource;
use Symfony\Cmf\Component\Resource\Description\Description;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
use Sylius\Component\Resource\Model\ResourceInterface;
use Prophecy\Argument;
use Symfony\Cmf\Component\Resource\Description\Descriptor;

class ResourceEnhancerTest extends \PHPUnit_Framework_TestCAse
{
public function setUp()
{
$this->registry = $this->prophesize(RegistryInterface::class);
$this->urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$this->requestConfigurationFactory = $this->prophesize(RequestConfigurationFactory::class);
$this->requestStack = $this->prophesize(RequestStack::class);

$this->enhancer = new ResourceEnhancer(
$this->registry->reveal(),
$this->requestStack->reveal(),
$this->requestConfigurationFactory->reveal(),
$this->urlGenerator->reveal()
);

$this->request = $this->prophesize(Request::class);
$this->cmfResource = $this->prophesize(CmfResource::class);
$this->puliResource = $this->prophesize(PuliResource::class);
$this->metadata = $this->prophesize(Metadata::class);
$this->requestConfiguration = $this->prophesize(RequestConfiguration::class);
$this->payload = $this->prophesize(ResourceInterface::class);
}

/**
* It should return true if the payload is supported by Sylius.
*/
public function testSupportedBySylius()
{
$this->cmfResource->getPayloadType()->willReturn('stdClass');
$this->registry->getByClass('stdClass')->willReturn($this->metadata->reveal());
$this->assertTrue(
$this->enhancer->supports($this->cmfResource->reveal())
);
}

/**
* It should return false if the payload is not supported by Sylius.
*/
public function testNotSupportedBySylius()
{
$this->cmfResource->getPayloadType()->willReturn('stdClass');
$this->registry->getByClass('stdClass')->willThrow(new \InvalidArgumentException('foo'));
$this->assertFalse(
$this->enhancer->supports($this->cmfResource->reveal())
);
}

/**
* It should return false if the resource is not a CmfResource.
*/
public function testResourceNotACmfResource()
{
$this->assertFalse(
$this->enhancer->supports($this->puliResource->reveal())
);
}


/**
* It should add sylius routes for a supported payload.
*/
public function testEnhance()
{
$description = new Description($this->cmfResource->reveal());
$this->cmfResource->getPayloadType()->willReturn('stdClass');
$this->cmfResource->getPayload()->willReturn($this->payload->reveal());
$this->payload->getId()->willReturn(5);

$this->registry->getByClass('stdClass')->willReturn($this->metadata->reveal());
$this->requestStack->getCurrentRequest()->willReturn($this->request->reveal());
$this->requestConfigurationFactory
->create($this->metadata->reveal(), $this->request->reveal())
->willReturn($this->requestConfiguration->reveal());
$this->requestConfiguration->getRouteName(Argument::type('string'))->will(function ($args) {
return $args[0];
});
$this->urlGenerator->generate(Argument::type('string'), Argument::type('array'))->will(function ($args) {
return $args[0] . '/' . $args[1]['id'];
});

$this->enhancer->enhance($description);

$this->assertEquals([
Descriptor::LINK_SHOW_HTML => 'show/5',
Descriptor::LINK_LIST_HTML => 'index/5',
Descriptor::LINK_EDIT_HTML => 'update/5',
Descriptor::LINK_CREATE_HTML => 'create/5',
Descriptor::LINK_REMOVE_HTML => 'delete/5',
], $description->all());
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"doctrine/phpcr-odm": "~1.2",
"jackalope/jackalope-fs": "dev-master",
"sonata-project/admin-bundle": "^3.1",
"sylius/resource": "0.18"
"sylius/resource-bundle": "0.18"
},
"suggest": {
"doctrine/phpcr-odm": "To enable support for the PHPCR ODM documents",
Expand Down

0 comments on commit 4df78ef

Please sign in to comment.