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

Commit

Permalink
Admin metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed May 23, 2016
1 parent 7bbae2d commit fdd8ff6
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Metadata/PayloadMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Resource\Metadata;

class PayloadMetadata
{
private $metadata = [];
private $supported = [];

// shorthand reference for resource, e.g. app.page
const TYPE_ALIAS = 'alias';

// title for the resource type, e.g. "Pages"
const TYPE_TITLE = 'title.type';

// title to use for the resource
const TITLE = 'title.type';

// HTML links
const LINK_EDIT_HTML = 'link.edit.html';
const LINK_CREATE_HTML = 'link.create.html';
const LINK_UPDATE_HTML = 'link.update.html';
const LINK_REMOVE_HTML = 'link.remove.html';
const LINK_SHOW_HTML = 'link.show.html';

// REST links
const LINK_EDIT_REST = 'link.edit.rest';
const LINK_CREATE_REST = 'link.create.rest';
const LINK_UPDATE_REST = 'link.update.rest';
const LINK_REMOVE_REST = 'link.remove.rest';
const LINK_SHOW_REST = 'link.show.rest';

public function __construct($providerName, array $metadata)
{
$this->providerName = $providerName;
$this->metadata = $metadata;
}

/**
* Return the metadata value for the given key.
*
* The key should be one of the constants defined in this class.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
if (!isset($this->supported[$key])) {
throw new \InvalidArgumentException(sprintf(
'Metadata key "%s" from "%s" not supported',
$key,
$this->providerName
));
}

return $this->metadata[$key];
}
}
22 changes: 22 additions & 0 deletions Metadata/PayloadMetadataFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Resource\Metadata;

use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;

interface PayloadMetadataFactoryInterface
{
/**
* Return the metadata for the given payload.
*/
public function getPayloadMetadata(CmfResource $resource);
}
92 changes: 92 additions & 0 deletions Metadata/SonataMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Resource\Metadata;

use Symfony\Cmf\Component\Resource\Repository\PayloadMetadataFactoryInterface;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Symfony\Cmf\Component\Resource\Repository\PayloadMetadata;

/**
* Add links and meta-info from Sonata Admin
*
* @author Daniel Leech <[email protected]>
*/
class SonataMetadataFactory implements PayloadMetadataFactoryInterface
{
/**
* @var Pool
*/
private $pool;

/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;

/**
* __construct
*
* @param Pool $pool
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(Pool $pool, UrlGeneratorInterface $urlGenerator)
{
$this->pool = $pool;
$this->urlGenerator = $urlGenerator;
}

/**
* {@inheritDoc}
*/
public function get(CmfResource $resource)
{
$object = $resource->getPayload();

// sonata has dependency on ClassUtils so this is fine.
$class = ClassUtils::getClass($object);

if (false === $this->pool->hasAdminByClass($class)) {
return $data;
}

$admin = $this->pool->getAdminByClass($class);

$links = array();

$routeCollection = $admin->getRoutes();

foreach ($routeCollection->getElements() as $code => $route) {
$routeName = $route->getDefault('_sonata_name');
$url = $this->urlGenerator->generate($routeName, array(
$admin->getIdParameter() => $admin->getUrlsafeIdentifier($object),
), true);

$routeRole = substr($code, strlen($admin->getCode()) + 1);

$links[$routeRole] = $url;
}

$metadata = new PayloadMetadata(
'sonata',
[
PayloadMetadata::TITLE => $admin->toString($object),
PayloadMetadata::TYPE_TITLE => $admin->getLabel(),
PayloadMetadata::LINK_EDIT_HTML => $links['edit'],
PayloadMetadata::LINK_UPDATE_HTML => $links['update'],
PayloadMetadata::LINK_CREATE_HTML => $links['create'],
PayloadMetadata::LINK_REMOVE_HTML => $links['remove'],
]
);

return $metadata;
}
}
77 changes: 77 additions & 0 deletions Metadata/SyliusMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Resource\Metadata;

use Symfony\Cmf\Component\Resource\Repository\PayloadMetadataFactoryInterface;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Symfony\Cmf\Component\Resource\Repository\PayloadMetadata;

/**
* Add links and meta-info from Sonata Admin
*
* @author Daniel Leech <[email protected]>
*/
class SyliusMetadataFactory implements PayloadMetadataFactoryInterface
{
/**
* @var Registry
*/
private $registry;

/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;

/**
* __construct
*
* @param Registry $registry
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(Registry $registry, UrlGeneratorInterface $urlGenerator)
{
$this->registry = $registry;
$this->urlGenerator = $urlGenerator;
}

/**
* {@inheritDoc}
*/
public function get(CmfResource $resource)
{
$object = $resource->getPayload();

// sonata has dependency on ClassUtils so this is fine.
$class = ClassUtils::getClass($object);

// TODO: does this already throw an exception?
$metadata = $this->registry->getMetadataForClass($class);

$metadata = new PayloadMetadata(
'sonata',
[
PayloadMetadata::ALIAS => $metadata->getAlias(),
// PayloadMetadata::TITLE => TODO: Is this supported?
PayloadMetadata::TYPE_TITLE => $metadata->getAlias(), // maybe run this through the translator

// TODO: Links are by convention
PayloadMetadata::LINK_EDIT_HTML => null,
PayloadMetadata::LINK_UPDATE_HTML => null,
PayloadMetadata::LINK_CREATE_HTML => null,
PayloadMetadata::LINK_REMOVE_HTML => null,
]
);

return $metadata;
}
}

0 comments on commit fdd8ff6

Please sign in to comment.