/**
  * @inheritdoc
  */
 public function getUrl(ResourceObjectInterface $resource)
 {
     $url = $this->getLocationConfig('url', $resource->getLocation(), $this->config);
     preg_match_all('/\\{(\\w+)\\}/', $url, $matches);
     $accessor = new PropertyAccessor();
     if (isset($matches[1])) {
         foreach ($matches[1] as $token) {
             if ($token === 'id') {
                 //when mapping information contains {id}
                 //for security reasons instead of set the real resource id
                 //set a random value and save in session with the real id
                 //the param converter resolve the real resource related for given hash
                 //and keep the resource private for non public access
                 $value = md5(mt_rand());
                 $this->session->set('_resource/' . $value, $resource->getId());
             } else {
                 if ($accessor->isReadable($resource, $token)) {
                     $value = $accessor->getValue($resource, $token);
                 } else {
                     $msg = sprintf('Invalid parameter "{%s}" in %s resource mapping.', $token, $resource->getLocation());
                     throw new \InvalidArgumentException($msg);
                 }
             }
             $url = str_replace("{{$token}}", $value, $url);
         }
     }
     return str_replace('//', '/', $url);
 }
示例#2
0
 /**
  * Set the name of resource using the context and resource mapping information
  *
  * @param ResourceObjectInterface $resource
  * @param string                  $baseName default name
  * @param object                  $context  context to resolve tokens
  */
 protected function setResourceName(ResourceObjectInterface $resource, $baseName = null, $context = null)
 {
     if ($baseName) {
         if (strpos($baseName, $resource->getFile()->guessExtension()) === false) {
             $filename = $baseName . '.' . $resource->getFile()->guessExtension();
         } else {
             $filename = $baseName;
         }
     } else {
         if ($resource->getId()) {
             $filename = $resource->getName();
         } else {
             //create unique name
             $filename = sha1(uniqid(mt_rand(), true)) . '.' . $resource->getFile()->guessExtension();
         }
     }
     $mapping = $this->getResourceMapping($this->config, $resource);
     if (isset($mapping['name'])) {
         $filename = $this->parseName($mapping['name'], $context, $baseName);
         $filename .= '.' . $resource->getFile()->guessExtension();
         $resource->setName($filename);
         $filename = basename($filename);
     }
     $resource->setName($filename);
 }