The methods in this class are able to deal with URLs.
Since: 2.3
Author: Bernhard Schussek (bschussek@gmail.com)
Author: Claudio Zizza (claudio@budgegeria.de)
 /**
  * {@inheritdoc}
  */
 public function generateUrl($repositoryPath, $currentUrl = null)
 {
     $matchedBinding = null;
     $bindings = $this->discovery->findBindings(self::BINDING_TYPE);
     foreach ($bindings as $binding) {
         /** @var ResourceBinding $binding */
         if (Glob::match($repositoryPath, $binding->getQuery())) {
             $matchedBinding = $binding;
             break;
         }
     }
     if (null === $matchedBinding) {
         throw new CannotGenerateUrlException(sprintf('Cannot generate URL for "%s". The path is not public.', $repositoryPath));
     }
     // We can't prevent a resource to be mapped to more than one public path
     // For now, we'll just take the first one and make the user responsible
     // for preventing duplicates
     $url = $this->generateUrlForBinding($matchedBinding, $repositoryPath);
     if ($currentUrl) {
         try {
             $url = Url::makeRelative($url, $currentUrl);
         } catch (InvalidArgumentException $e) {
             throw new CannotGenerateUrlException(sprintf('Cannot generate URL for "%s" to current url "%s".', $repositoryPath, $currentUrl), $e->getCode(), $e);
         }
     }
     return $url;
 }
Example #2
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage The URL "http://example.com" cannot be made relative to "http://example2.com" since
  *                           their host names are different.
  * @covers Webmozart\PathUtil\Url
  */
 public function testMakeRelativeFailsIfDifferentDomains()
 {
     Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'http://example2.com/webmozart/puli');
 }