Symfony\Component\HttpKernel\Kernel::locateResource PHP Method

locateResource() public method

A Resource can be a file or a directory. The resource name must follow the following pattern: @/path/to/a/file.something where BundleName is the name of the bundle and the remaining part is the relative path in the bundle. If $dir is passed, and the first segment of the path is "Resources", this method will look for a file named: $dir//path/without/Resources before looking in the bundle resource folder.
public locateResource ( string $name, string $dir = null, boolean $first = true ) : string | array
$name string A resource name to locate
$dir string A directory where to look for the resource first
$first boolean Whether to return the first path or paths for all matching bundles
return string | array The absolute path of the resource or an array if $first is false
    public function locateResource($name, $dir = null, $first = true)
    {
        if ('@' !== $name[0]) {
            throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
        }

        if (false !== strpos($name, '..')) {
            throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
        }

        $bundleName = substr($name, 1);
        $path = '';
        if (false !== strpos($bundleName, '/')) {
            list($bundleName, $path) = explode('/', $bundleName, 2);
        }

        $isResource = 0 === strpos($path, 'Resources') && null !== $dir;
        $overridePath = substr($path, 9);
        $resourceBundle = null;
        $bundles = $this->getBundle($bundleName, false);
        $files = array();

        foreach ($bundles as $bundle) {
            if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) {
                if (null !== $resourceBundle) {
                    throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived bundle. Create a "%s" file to override the bundle resource.',
                        $file,
                        $resourceBundle,
                        $dir.'/'.$bundles[0]->getName().$overridePath
                    ));
                }

                if ($first) {
                    return $file;
                }
                $files[] = $file;
            }

            if (file_exists($file = $bundle->getPath().'/'.$path)) {
                if ($first && !$isResource) {
                    return $file;
                }
                $files[] = $file;
                $resourceBundle = $bundle->getName();
            }
        }

        if (count($files) > 0) {
            return $first && $isResource ? $files[0] : $files;
        }

        throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name));
    }

Usage Example

 public function getFilters()
 {
     $client = new \Mleko\ImageSqueeze\Client\Client();
     $squeeze = new \Twig_SimpleFilter('squeeze', function ($path) use($client) {
         $path = (string) $path;
         if (strlen($path) > 0 && $path[0] === '@') {
             $path = $this->kernel->locateResource($path);
         }
         if (file_exists($path)) {
             $inputFile = new \Mleko\ImageSqueeze\Client\File($path);
         } else {
             $inputFile = new \Mleko\ImageSqueeze\Client\File($path, $this->webRoot);
         }
         $pathHash = str_pad(base_convert(sha1($path), 16, 36), 31, '0', STR_PAD_LEFT);
         $compressedName = implode("/", str_split(substr($pathHash, 0, 6), 1)) . "/" . substr($pathHash, 6);
         if (false !== ($dotPosition = strrpos($path, "."))) {
             $compressedName .= substr($path, $dotPosition);
         }
         $newPath = '/cache/image/' . $compressedName;
         $fullPath = $this->webRoot . $newPath;
         if (!file_exists($fullPath)) {
             $newDir = dirname($fullPath);
             if (!file_exists($newDir)) {
                 mkdir($newDir, 0777, true);
             }
             return $client->shrink($inputFile)->toFile($newPath, $this->webRoot);
         }
         return new \Mleko\ImageSqueeze\Client\File($newPath, $this->webRoot);
     });
     return ['squeeze' => $squeeze];
 }
All Usage Examples Of Symfony\Component\HttpKernel\Kernel::locateResource