makeRelative() public static method

The result is a canonical path. This class is using functionality of Path class.
See also: Path
public static makeRelative ( string $url, string $baseUrl ) : string
$url string A URL to make relative.
$baseUrl string A base URL.
return string
 /**
  * {@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;
 }
Beispiel #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');
 }