Neos\Fusion\TypoScriptObjects\ResourceUriImplementation::evaluate PHP Method

evaluate() public method

Returns the absolute URL of a resource
public evaluate ( ) : string
return string
    public function evaluate()
    {
        $resource = $this->getResource();
        if ($resource !== null) {
            $uri = false;
            if ($resource instanceof PersistentResource) {
                $uri = $this->resourceManager->getPublicPersistentResourceUri($resource);
            }
            if ($uri === false) {
                throw new TypoScriptException('The specified resource is invalid', 1386458728);
            }
            return $uri;
        }
        $path = $this->getPath();
        if ($path === null) {
            throw new TypoScriptException('Neither "resource" nor "path" were specified', 1386458763);
        }
        if (strpos($path, 'resource://') === 0) {
            $matches = array();
            if (preg_match('#^resource://([^/]+)/Public/(.*)#', $path, $matches) !== 1) {
                throw new TypoScriptException(sprintf('The specified path "%s" does not point to a public resource.', $path), 1386458851);
            }
            $package = $matches[1];
            $path = $matches[2];
        } else {
            $package = $this->getPackage();
            if ($package === null) {
                $controllerContext = $this->tsRuntime->getControllerContext();
                /** @var $actionRequest ActionRequest */
                $actionRequest = $controllerContext->getRequest();
                $package = $actionRequest->getControllerPackageKey();
            }
        }
        $localize = $this->isLocalize();
        if ($localize === true) {
            $resourcePath = 'resource://' . $package . '/Public/' . $path;
            $localizedResourcePathData = $this->i18nService->getLocalizedFilename($resourcePath);
            $matches = array();
            if (preg_match('#resource://([^/]+)/Public/(.*)#', current($localizedResourcePathData), $matches) === 1) {
                $package = $matches[1];
                $path = $matches[2];
            }
        }
        return $this->resourceManager->getPublicPackageResourceUri($package, $path);
    }

Usage Example

 /**
  * @test
  */
 public function evaluateLocalizesFilenameIfLocalize()
 {
     $this->mockTsRuntime->expects($this->any())->method('evaluate')->will($this->returnCallback(function ($evaluatePath, $that) {
         $relativePath = str_replace('resourceUri/test/', '', $evaluatePath);
         switch ($relativePath) {
             case 'localize':
                 return true;
             case 'path':
                 return 'resource://Some.Package/Public/SomeResource';
             case 'package':
                 return 'Specified.Package';
         }
         return null;
     }));
     $this->mockI18nService->expects($this->atLeastOnce())->method('getLocalizedFilename')->will($this->returnValue(array('resource://Some.Package/Public/LocalizedFilename')));
     $this->mockResourceManager->expects($this->atLeastOnce())->method('getPublicPackageResourceUri')->will($this->returnValue('Static/Resources/Packages/Some.Package/LocalizedFilename'));
     $this->assertSame('Static/Resources/Packages/Some.Package/LocalizedFilename', $this->resourceUriImplementation->evaluate());
 }