This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
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,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]; | ||
} | ||
} |
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,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); | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |