/**
  * @param  \ZB\Utils\Entity\IdProviderInterface $idProvider
  * @param  \ZF\Hal\Plugin\Hal $halPlugin
  * @return \ZF\Hal\Entity
  */
 public function convertToHalEntity(IdProviderInterface $idProvider, HalPlugin $halPlugin)
 {
     $halMetadata = $halPlugin->getMetadataMap()->get($idProvider);
     $halEntity = new HalEntity($idProvider);
     $halEntity->getLinks()->add(HalLink::factory(array('rel' => 'self', 'route' => array('name' => $halMetadata->getRoute(), 'options' => $halMetadata->getRouteOptions(), 'params' => array_merge($halMetadata->getRouteParams(), array($halMetadata->getRouteIdentifierName() => $idProvider->getId()))))));
     return $halEntity;
 }
Example #2
0
 public function setUp()
 {
     $this->controller = $controller = new RestController();
     $this->router = $router = new SimpleRouteStack();
     $route = new Segment('/resource[/[:id]]');
     $router->addRoute('resource', $route);
     $this->event = $event = new MvcEvent();
     $event->setRouter($router);
     $event->setRouteMatch(new RouteMatch(array()));
     $controller->setEvent($event);
     $controller->setRoute('resource');
     $pluginManager = new PluginManager();
     $pluginManager->setService('bodyParams', new BodyParams());
     $controller->setPluginManager($pluginManager);
     $urlHelper = new UrlHelper();
     $urlHelper->setRouter($this->router);
     $serverUrlHelper = new ServerUrlHelper();
     $serverUrlHelper->setScheme('http');
     $serverUrlHelper->setHost('localhost.localdomain');
     $linksHelper = new HalHelper();
     $linksHelper->setUrlHelper($urlHelper);
     $linksHelper->setServerUrlHelper($serverUrlHelper);
     $pluginManager->setService('Hal', $linksHelper);
     $linksHelper->setController($controller);
     $this->resource = $resource = new Resource();
     $controller->setResource($resource);
 }
    public function setupHelpers()
    {
        if (!$this->router) {
            $this->setupRouter();
        }

        $urlHelper = new UrlHelper();
        $urlHelper->setRouter($this->router);

        $serverUrlHelper = new ServerUrlHelper();
        $serverUrlHelper->setScheme('http');
        $serverUrlHelper->setHost('localhost.localdomain');

        $linksHelper = new HalHelper();
        $linksHelper->setUrlHelper($urlHelper);
        $linksHelper->setServerUrlHelper($serverUrlHelper);

        $this->helpers = $helpers = new HelperPluginManager();
        $helpers->setService('url', $urlHelper);
        $helpers->setService('serverUrl', $serverUrlHelper);
        $helpers->setService('hal', $linksHelper);

        $this->plugins = $plugins = new ControllerPluginManager();
        $plugins->setService('hal', $linksHelper);
    }
Example #4
0
 /**
  * @param  ContainerInterface|\Zend\ServiceManager\ServiceLocatorInterface $container
  * @return Plugin\Hal
  */
 public function __invoke(ContainerInterface $container)
 {
     $container = $container instanceof AbstractPluginManager ? $container->getServiceLocator() : $container;
     /* @var $rendererOptions \ZF\Hal\RendererOptions */
     $rendererOptions = $container->get('ZF\\Hal\\RendererOptions');
     $metadataMap = $container->get('ZF\\Hal\\MetadataMap');
     /** @var HydratorPluginManager $hydrators */
     $hydrators = $metadataMap->getHydratorManager();
     $helper = new Plugin\Hal($hydrators);
     $helper->setMetadataMap($metadataMap);
     $linkUrlBuilder = $container->get(Link\LinkUrlBuilder::class);
     $helper->setLinkUrlBuilder($linkUrlBuilder);
     $linkCollectionExtractor = $container->get(LinkCollectionExtractor::class);
     $helper->setLinkCollectionExtractor($linkCollectionExtractor);
     $defaultHydrator = $rendererOptions->getDefaultHydrator();
     if ($defaultHydrator) {
         if (!$hydrators->has($defaultHydrator)) {
             throw new Exception\DomainException(sprintf('Cannot locate default hydrator by name "%s" via the HydratorManager', $defaultHydrator));
         }
         /** @var HydratorInterface $hydrator */
         $hydrator = $hydrators->get($defaultHydrator);
         $helper->setDefaultHydrator($hydrator);
     }
     $helper->setRenderEmbeddedEntities($rendererOptions->getRenderEmbeddedEntities());
     $helper->setRenderCollections($rendererOptions->getRenderEmbeddedCollections());
     $hydratorMap = $rendererOptions->getHydrators();
     foreach ($hydratorMap as $class => $hydratorServiceName) {
         $helper->addHydrator($class, $hydratorServiceName);
     }
     return $helper;
 }
 /**
  * @param  ServiceLocatorInterface $serviceLocator
  * @return Plugin\Hal
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $services = $serviceLocator->getServiceLocator();
     $halConfig = $services->get('ZF\\Hal\\HalConfig');
     /* @var $rendererOptions \ZF\Hal\RendererOptions */
     $rendererOptions = $services->get('ZF\\Hal\\RendererOptions');
     $metadataMap = $services->get('ZF\\Hal\\MetadataMap');
     $hydrators = $metadataMap->getHydratorManager();
     $serverUrlHelper = $serviceLocator->get('ServerUrl');
     if (isset($halConfig['options']['use_proxy'])) {
         $serverUrlHelper->setUseProxy($halConfig['options']['use_proxy']);
     }
     $urlHelper = $serviceLocator->get('Url');
     $helper = new Plugin\Hal($hydrators);
     $helper->setMetadataMap($metadataMap)->setServerUrlHelper($serverUrlHelper)->setUrlHelper($urlHelper);
     $linkExtractor = new LinkExtractor($serverUrlHelper, $urlHelper);
     $linkCollectionExtractor = new LinkCollectionExtractor($linkExtractor);
     $helper->setLinkCollectionExtractor($linkCollectionExtractor);
     $defaultHydrator = $rendererOptions->getDefaultHydrator();
     if ($defaultHydrator) {
         if (!$hydrators->has($defaultHydrator)) {
             throw new Exception\DomainException(sprintf('Cannot locate default hydrator by name "%s" via the HydratorManager', $defaultHydrator));
         }
         $hydrator = $hydrators->get($defaultHydrator);
         $helper->setDefaultHydrator($hydrator);
     }
     $helper->setRenderEmbeddedEntities($rendererOptions->getRenderEmbeddedEntities());
     $helper->setRenderCollections($rendererOptions->getRenderEmbeddedCollections());
     $helper->setUseResourceLinksInsteadOfEmbeddedEntityLink($rendererOptions->getUseResourceLinksInsteadOfEmbeddedEntityLink());
     $hydratorMap = $rendererOptions->getHydrators();
     foreach ($hydratorMap as $class => $hydratorServiceName) {
         $helper->addHydrator($class, $hydratorServiceName);
     }
     return $helper;
 }
Example #6
0
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return Plugin\Hal
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $services        = $serviceLocator->getServiceLocator();
        $config          = $services->get('Config');
        $metadataMap     = $services->get('ZF\Hal\MetadataMap');
        $hydrators       = $metadataMap->getHydratorManager();

        $serverUrlHelper = $serviceLocator->get('ServerUrl');
        if (isset($config['zf-hal']['options']['use_proxy'])) {
            $serverUrlHelper->setUseProxy($config['zf-hal']['options']['use_proxy']);
        }
        $urlHelper       = $serviceLocator->get('Url');

        $helper = new Plugin\Hal($hydrators);
        $helper
            ->setMetadataMap($metadataMap)
            ->setServerUrlHelper($serverUrlHelper)
            ->setUrlHelper($urlHelper);

        if (isset($config['zf-hal'])
            && isset($config['zf-hal']['renderer'])
        ) {
            $config = $config['zf-hal']['renderer'];

            if (isset($config['default_hydrator'])) {
                $hydratorServiceName = $config['default_hydrator'];

                if (!$hydrators->has($hydratorServiceName)) {
                    throw new Exception\DomainException(sprintf(
                        'Cannot locate default hydrator by name "%s" via the HydratorManager',
                        $hydratorServiceName
                    ));
                }

                $hydrator = $hydrators->get($hydratorServiceName);
                $helper->setDefaultHydrator($hydrator);
            }

            if (isset($config['render_embedded_resources'])) {
                $helper->setRenderEmbeddedEntities($config['render_embedded_resources']);
            }

            if (isset($config['render_embedded_entities'])) {
                $helper->setRenderEmbeddedEntities($config['render_embedded_entities']);
            }

            if (isset($config['render_collections'])) {
                $helper->setRenderCollections($config['render_collections']);
            }

            if (isset($config['hydrators']) && is_array($config['hydrators'])) {
                $hydratorMap = $config['hydrators'];
                foreach ($hydratorMap as $class => $hydratorServiceName) {
                    $helper->addHydrator($class, $hydratorServiceName);
                }
            }
        }

        return $helper;
    }
 public function setUpHelpers()
 {
     if (isset($this->helpers)) {
         return;
     }
     $this->setupRouter();
     $urlHelper = new UrlHelper();
     $urlHelper->setRouter($this->router);
     $serverUrlHelper = new ServerUrlHelper();
     $serverUrlHelper->setScheme('http');
     $serverUrlHelper->setHost('localhost.localdomain');
     $this->linksHelper = $linksHelper = new HalHelper();
     $linksHelper->setUrlHelper($urlHelper);
     $linksHelper->setServerUrlHelper($serverUrlHelper);
     $this->helpers = $helpers = new HelperPluginManager();
     $helpers->setService('url', $urlHelper);
     $helpers->setService('serverUrl', $serverUrlHelper);
     $helpers->setService('hal', $linksHelper);
 }
Example #8
0
 /**
  * @group 100
  */
 public function testRenderEntityPostEventIsTriggered()
 {
     $entity = ['id' => 1, 'foo' => 'bar'];
     $halEntity = new Entity($entity, 1);
     $triggered = false;
     $this->plugin->getEventManager()->attach('renderEntity.post', function ($e) use(&$triggered) {
         $triggered = true;
     });
     $this->plugin->renderEntity($halEntity);
     $this->assertTrue($triggered);
 }
Example #9
0
 /**
  * @expectedException \Zend\Mvc\Router\Exception\RuntimeException
  */
 public function testNotExistingRouteInMetadataLinks()
 {
     $object = new TestAsset\Entity('foo', 'Foo');
     $object->first_child = new TestAsset\EmbeddedEntity('bar', 'Bar');
     $entity = new Entity($object, 'foo');
     $self = new Link('self');
     $self->setRoute('hostname/resource', ['id' => 'foo']);
     $entity->getLinks()->add($self);
     $metadata = new MetadataMap(['ZFTest\\Hal\\Plugin\\TestAsset\\EmbeddedEntity' => ['hydrator' => 'Zend\\Hydrator\\ObjectProperty', 'route' => 'hostname/embedded', 'route_identifier_name' => 'id', 'entity_identifier_name' => 'id', 'links' => ['link' => ['rel' => 'link', 'route' => ['name' => 'non_existing_route']]]]]);
     $this->plugin->setMetadataMap($metadata);
     $this->plugin->renderEntity($entity);
 }
 public function setUp()
 {
     $this->router = $router = new TreeRouteStack();
     $route = new Segment('/resource[/[:id]]');
     $router->addRoute('resource', $route);
     $route2 = new Segment('/help');
     $router->addRoute('docs', $route2);
     $router->addRoute('hostname', array('type' => 'hostname', 'options' => array('route' => 'localhost.localdomain'), 'child_routes' => array('resource' => array('type' => 'segment', 'options' => array('route' => '/resource[/:id]'), 'may_terminate' => true, 'child_routes' => array('children' => array('type' => 'literal', 'options' => array('route' => '/children')))), 'users' => array('type' => 'segment', 'options' => array('route' => '/users[/:id]')), 'contacts' => array('type' => 'segment', 'options' => array('route' => '/contacts[/:id]')), 'embedded' => array('type' => 'segment', 'options' => array('route' => '/embedded[/:id]')), 'embedded_custom' => array('type' => 'segment', 'options' => array('route' => '/embedded_custom[/:custom_id]')))));
     $this->event = $event = new MvcEvent();
     $event->setRouter($router);
     $router->setRequestUri(new Http('http://localhost.localdomain/resource'));
     $controller = $this->controller = $this->getMock('Zend\\Mvc\\Controller\\AbstractRestfulController');
     $controller->expects($this->any())->method('getEvent')->will($this->returnValue($event));
     $this->urlHelper = $urlHelper = new UrlHelper();
     $urlHelper->setRouter($router);
     $this->serverUrlHelper = $serverUrlHelper = new ServerUrlHelper();
     $serverUrlHelper->setScheme('http');
     $serverUrlHelper->setHost('localhost.localdomain');
     $this->plugin = $plugin = new HalHelper();
     $plugin->setController($controller);
     $plugin->setUrlHelper($urlHelper);
     $plugin->setServerUrlHelper($serverUrlHelper);
 }
Example #11
0
    /**
     * @param $collection
     * @param $metadataMap
     * @param $expectedResult
     * @param $exception
     *
     * @dataProvider renderCollectionWithMaxDepthProvider
     */
    public function testRenderCollectionWithMaxDepth($collection, $metadataMap, $expectedResult, $exception = null)
    {
        $this->plugin->setMetadataMap($metadataMap);

        if ($exception) {
            $this->setExpectedException($exception['class'], $exception['message']);
        }

        if (is_callable($collection)) {
            $collection = $collection();
        }

        $halCollection = $this->plugin->createCollection($collection);
        $result = $this->plugin->renderCollection($halCollection);

        $this->assertEquals($expectedResult, $result);
    }
 public function setUpHelpers()
 {
     // need to setup routes
     // need to get a url and serverurl helper that have appropriate injections
     $this->router = $router = new TreeRouteStack();
     $this->entityRoute = new Segment('/resource[/[:id]]');
     $this->router->addRoute('resource', $this->entityRoute);
     $this->helpers = $helpers = new HelperPluginManager();
     $serverUrl = $helpers->get('ServerUrl');
     $url = $helpers->get('url');
     $url->setRouter($router);
     $serverUrl->setScheme('http');
     $serverUrl->setHost('localhost.localdomain');
     $halLinks = new HalHelper();
     $halLinks->setServerUrlHelper($serverUrl);
     $halLinks->setUrlHelper($url);
     $helpers->setService('Hal', $halLinks);
     $this->renderer->setHelperPluginManager($helpers);
 }