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

Example for admin metadata implementation, #19

Merged
merged 7 commits into from
Jun 5, 2016
Merged

Conversation

dantleech
Copy link
Member

@dantleech dantleech commented May 23, 2016

See #18

Descriptors

A description is just an key / value store and passed to any number of
supprorting "enrichers" (e.g. SonataEnricher, SyliusEnricher) which add any
information.

In order to ensure interoperability a Descriptor class is given which
provides constant values for use as descriptor keys, e.g. LINK_EDIT_HTML,
TYPE_ALIAS, TITLE.

Use is as follows:

$factory = new DescriptionFactory([
    new SyliusEnricher(...),  // add links for Sylius
    new IconEnricher()        // add "icon" URLs (as an example)
]); 

$resource = $registry->get('/pages/foobar/my-article'); // get a resource
$description = $factory->getPayloadDescriptionFor($resource);

echo $description->get(Descriptor::TITLE); // "My Article"
echo $description->get(Descriptor::TYPE); // AcmeBundle\Document\Article
echo $description->has(Descriptor::LINK_HTML_EDIT); // true
echo $description->get(Descriptor::LINK_HTML_EDIT); // /path/to/edit/page/123

Enhancers

Enhancers included in this PR:

  • SonataAdminEnhancer: Adds CRUD URLs for resources.
  • SyliusEnhancer: Adds CRUD URLs for resources.

*/
public function get($key)
{
if (!isset($this->supported[$key])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would you fill that array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was just junk from the WIP.

@dantleech dantleech force-pushed the admin_metadata branch 2 times, most recently from 16c5c92 to 719c836 Compare May 24, 2016 13:59

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

interface DescriptionEnricherInterface
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming this with the more apt term (imo) "augmenter"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or use Enhancer as with the RoutingBundle - I think the semantic is the same /cc @dbu

public function getPayloadDescriptionFor(CmfResource $resource)
{
$type = $resource->getPayloadType();
$payload = $resource->getPayload();
Copy link
Member

@wouterj wouterj May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to check if payload is an object. At the moment, it can be anything: https://github.com/symfony-cmf/resource/blob/master/Repository/Resource/CmfResource.php#L40-L48 (I even believe we use it as string in the ResourceBundle atm)

That was a wrong comment. However, I don't see why we have to know the payload? Removing this and injecting the resource in the enricher would allow us to use any puli resource.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we would need to inject tge Resource into the Description, making it a Resource Description and not a Payload Description (what I see it as now) but, what you suggest probably makes more sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, the Description now accepts a PuliResource and we pass only the Description to the enhance (renamed from enrich) method of the Enhancer.

@dantleech dantleech changed the title [RFC] Example for admin metadata implementation, Example for admin metadata implementation, May 25, 2016
/**
* Return the descriptors value for the given key.
*
* The key should be one of the constants defined in this class.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not "this" class.

@dantleech dantleech force-pushed the admin_metadata branch 2 times, most recently from 9932069 to b11a77c Compare May 25, 2016 08:55
public function set($key, $value)
{
$this->descriptors[$key] = $value;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about also implementing the other methods mentioned in http://symfony.com/doc/current/contributing/code/conventions.html#method-names ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we are missing has() and possibly all().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something to set several descriptors in one call, by providing a hashmap that is merged with $this->descriptors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that would be merge() then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or setDescriptor($key, $value) and setDescriptors($descriptors)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But would setDescriptors replace all the descriptors or merge them? I would assume without prior knowledge that it would replace them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, true. maybe addDescriptors? mergeDescriptors feels strange. that adding something that already exists overwrites the existing value seems no big surprise to me, and we can state it explicitly in the phpdoc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer merge, as that correlates with array_merge which is what you are actually doing. However, I think the ParameterBag uses add() with the same semantic.

I guess suffixing Descriptor makes sense.

Copy link
Member

@dbu dbu May 27, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wouterj
Copy link
Member

wouterj commented May 25, 2016

Although I understand the reasons behind the Descriptor constants, I'm afraid blocks the current limits the amount of options that enhancers can edit.

Also, why not add these constants to the Description class?

@dantleech
Copy link
Member Author

Although I understand the reasons behind the Descriptor constants, I'm afraid blocks the current limits the amount of options that enhancers can edit.

My main concern is to try and avoid a situation where one implementation uses a different key/descriptor than another implementation for the same thing.

But I don't really see any alternative to defining a canonical set of "descriptors". How else could we "solve" this problem?

Also, why not add these constants to the Description class?

It just seemed a little bit more semantic:

$description->set(Descriptor::TITLE, 'Hello');
$description->set(Descriptor::LINK_EDIT_HTML, 'http://foo/bar.html');

vs.

$description->set(Description::TITLE, 'Hello');
$description->set(Description::LINK_EDIT_HTML, 'http://foo/bar.html');

and it separated the responsiblitiy of storing these values into a separate
class which just seems tidier.

Don't feel too strongly about it however.

class Description
{
/**
* @var array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the values in this array? strings, or something more complex? a list of label => url?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can limit them to be scalar values. If there is a use case for array values we could allow that later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should allow arrays also, f.e. to indicate the valid child payload types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@dantleech dantleech force-pushed the admin_metadata branch 2 times, most recently from ba30ba0 to 0b75002 Compare May 27, 2016 09:07
/**
* Return true if the provider supports the given type.
*
* @param CmfResource $resource
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Cmf/Puli

@dantleech
Copy link
Member Author

@wouterj could you have a look, I anticipate making further changes but I think it would be good to get this in to facilitate further work.

@wouterj
Copy link
Member

wouterj commented Jun 5, 2016

If everything works as expected from your point of view, let's just merge. We can improve it while it's in the master branch.

I agree with the concept behind this PR and don't see major problems in a quick review. I don't have time to test it atm.

@dantleech dantleech merged commit b676f3c into master Jun 5, 2016
@dantleech dantleech deleted the admin_metadata branch June 5, 2016 12:13
@dantleech dantleech removed the wip/poc label Jun 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants