/**
  * @param  ServiceLocatorInterface $serviceLocator
  * @return Plugin\JsonLD
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $services = $serviceLocator->getServiceLocator();
     $jsonLDConfig = $services->get('ZF\\JsonLD\\JsonLDConfig');
     /* @var $rendererOptions \ZF\JsonLD\RendererOptions */
     $rendererOptions = $services->get('ZF\\JsonLD\\RendererOptions');
     $metadataMap = $services->get('ZF\\JsonLD\\MetadataMap');
     $hydrators = $metadataMap->getHydratorManager();
     $serverUrlHelper = $serviceLocator->get('ServerUrl');
     if (isset($jsonLDConfig['options']['use_proxy'])) {
         $serverUrlHelper->setUseProxy($jsonLDConfig['options']['use_proxy']);
     }
     $urlHelper = $serviceLocator->get('Url');
     $helper = new Plugin\JsonLD($hydrators);
     $helper->setMetadataMap($metadataMap)->setServerUrlHelper($serverUrlHelper)->setUrlHelper($urlHelper);
     $propertyExtractor = new PropertyExtractor($serverUrlHelper, $urlHelper);
     $propertyCollectionExtractor = new PropertyCollectionExtractor($propertyExtractor);
     $helper->setPropertyCollectionExtractor($propertyCollectionExtractor);
     $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->getRenderMemberEntities());
     $helper->setRenderCollections($rendererOptions->getRenderMemberCollections());
     $hydratorMap = $rendererOptions->getHydrators();
     foreach ($hydratorMap as $class => $hydratorServiceName) {
         $helper->addHydrator($class, $hydratorServiceName);
     }
     return $helper;
 }
Beispiel #2
0
 /**
  * @group 100
  */
 public function testRenderEntityPostEventIsTriggered()
 {
     $entity = ['id' => 1, 'foo' => 'bar'];
     $jsonLDEntity = new Entity($entity, 1);
     $triggered = false;
     $this->plugin->getEventManager()->attach('renderEntity.post', function ($e) use(&$triggered) {
         $triggered = true;
     });
     $this->plugin->renderEntity($jsonLDEntity);
     $this->assertTrue($triggered);
 }
 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');
     $propertiesHelper = new JsonLDHelper();
     $propertiesHelper->setUrlHelper($urlHelper);
     $propertiesHelper->setServerUrlHelper($serverUrlHelper);
     $propertyExtractor = new PropertyExtractor($serverUrlHelper, $urlHelper);
     $propertyCollectionExtractor = new PropertyCollectionExtractor($propertyExtractor);
     $propertiesHelper->setPropertyCollectionExtractor($propertyCollectionExtractor);
     $this->helpers = $helpers = new HelperPluginManager();
     $helpers->setService('url', $urlHelper);
     $helpers->setService('serverUrl', $serverUrlHelper);
     $helpers->setService('JsonLD', $propertiesHelper);
     $this->plugins = $plugins = new ControllerPluginManager();
     $plugins->setService('JsonLD', $propertiesHelper);
 }