Ejemplo n.º 1
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use($options) {
         $data = $event->getData();
         $form = $event->getForm();
         if ($options['routing_strategy'] === Routing::STRATEGY_ID) {
             if (!method_exists($data, 'getId')) {
                 throw new \Exception('Routing strategy id used, but data has no getId method');
             }
         }
         if ($options['routing_strategy'] === Routing::STRATEGY_SLUG) {
             if (!$data instanceof Slugable || !method_exists($data, 'getSlug')) {
                 throw new \Exception('Routing strategy slug used, but data has no getSlug method nor is instanceof Slugable');
             }
             $form->add('slug', 'enhavo_slug');
         }
         if ($options['routing_strategy'] === Routing::STRATEGY_SLUG_ID) {
             if (!$data instanceof Slugable || !method_exists($data, 'getSlug')) {
                 throw new \Exception('Routing strategy id_slug used, but data has no getSlug method nor is instanceof Slugable');
             }
             if (!method_exists($data, 'getId')) {
                 throw new \Exception('Routing strategy id_slug used, but data has no getId method');
             }
             $form->add('slug', 'enhavo_slug', array());
         }
         if ($options['routing_strategy'] === Routing::STRATEGY_ROUTE) {
             if (!$data instanceof Routeable) {
                 throw new \Exception('Routing strategy route used, but data is not instanceof Routeable');
             }
             $form->add('route', 'enhavo_route');
         }
         if ($data) {
             try {
                 $url = $this->urlResolver->resolve($data, UrlGeneratorInterface::ABSOLUTE_URL);
                 $form->add('link', 'text', array('mapped' => false, 'data' => $url, 'read_only' => true));
             } catch (\InvalidArgumentException $e) {
                 return;
             }
         }
     });
     $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use($options) {
         if ($options['routing_strategy'] === Routing::STRATEGY_ROUTE) {
             $data = $event->getData();
             if ($data instanceof Routeable) {
                 $route = $data->getRoute();
                 if ($route instanceof Route && empty($route->getStaticPrefix())) {
                     /** @var GeneratorInterface $generator */
                     $generator = $this->container->get($options['routing_generator']);
                     $url = $generator->generate($data);
                     if ($url !== null) {
                         $route->setStaticPrefix($url);
                     }
                 }
             }
         }
     });
 }
Ejemplo n.º 2
0
 protected function convertToUrl(SitemapInterface $resource)
 {
     $url = new SitemapUrl();
     $url->setChangeFrequency($resource->getChangeFrequency());
     $url->setLastModified($resource->getUpdated());
     $url->setPriority($resource->getPriority());
     $url->setLocation($this->urlResolver->resolve($resource));
     return $url;
 }
Ejemplo n.º 3
0
 /**
  * @return string
  */
 public function resolveUrl($resource)
 {
     return $this->urlResolver->resolve($resource);
 }