PhlyRestfully icon indicating copy to clipboard operation
PhlyRestfully copied to clipboard

Ability to use metadata map without exact class name match

Open glennschmidt opened this issue 12 years ago • 1 comments

Currently the MetadataMap always does an exact class name comparison between get_class($your_resource) and the key in the map. It doesn't take inheritance into account. This prevents the map being used with Doctrine entities.

If you retrieve a MyModels\User instance from doctrine, get_class() might actually return DoctrineORMModule\Proxy\__CG__\MyModels\User, which is an auto-generated proxy subclass.

Could I suggest that PhlyRestfully either

  • Make MetadataMap check each map key against the resource with an instanceof test when the get_class comparison fails, or
  • Make it easier to supply a custom subclass of MetadataMap so that Doctrine users can implement this behaviour?

At the moment i'm making do by awkwardly overriding the definition of the 'PhlyRestfully\MetadataMap' service factory in my local module, to instantiate my MetadataMap subclass.

Here's the code I have in my subclass FYI:

    public function get($class)
    {
        //First try the default logic (look for an exact class-name match)
        if (parent::has($class))
            return parent::get($class);

        //Next check each class in the map to see if our object inherits from it
        if (is_object($class))
        {
            foreach ($this->map as $className => $metadata)
            {
                if ($class instanceof $className)
                    return $metadata;
            }
        }

        return null;
    }

glennschmidt avatar Oct 29 '13 11:10 glennschmidt

I solved this exact problem in my local source. But it is first time I want to contribute code, where should I start? :/

Waldz avatar Dec 07 '13 12:12 Waldz