Feature #33937

Convenience method to resolve public "resource://" paths

Added by Bastian Waidelich almost 9 years ago. Updated almost 7 years ago.

Status:
Accepted
Priority:
Should have
Category:
Resource
Target version:
-
Start date:
2012-02-15
Due date:
% Done:

0%

Estimated time:
PHP Version:
Has patch:
No
Complexity:

Description

It would be helpful to have an easy way to resolve the 'resource://...' path
to static resources.
Currently we end up using code like the following a lot:

protected function resolveResourcePath($resourcePath) {
    $matches = array();
    preg_match('#resource://([^/]*)/Public/(.*)#', $resourcePath, $matches);
    if ($matches === array()) {
        throw new \TYPO3\Fluid\Core\ViewHelper\Exception('Resource path "' . $resourcePath . '" can\'t be resolved.', 1328543327);
    }
    $packageKey = $matches[1];
    $path = $matches[2];
    return $this->resourcePublisher->getStaticResourcesWebBaseUri() . 'Packages/' . $packageKey . '/' . $path;
}

#1

Updated by Karsten Dambekalns almost 9 years ago

  • Status changed from New to Needs Feedback
  • Assignee set to Karsten Dambekalns

What is this needed for? Any file functions can use the resource:// URL directly. To fetch the web URL, a method exists already (no?).

#2

Updated by Bastian Waidelich almost 9 years ago

Ok, here's the ViewHelper which needed this:

<?php
namespace TYPO3\Form\ViewHelpers;

/*                                                                        *
 * This script belongs to the FLOW3 package "TYPO3.Form".                 *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 *  of the License, or (at your option) any later version.                *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

use TYPO3\FLOW3\Annotations as FLOW3;

/**
 * Output the configured stylesheets and JavaScript include tags for a given preset
 */
class RenderHeadViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
     * @FLOW3\Inject
     * @var \TYPO3\FLOW3\Resource\Publishing\ResourcePublisher
     */
    protected $resourcePublisher;

    /**
     * @FLOW3\Inject
     * @var \TYPO3\Form\Factory\ArrayFormFactory
     */
    protected $formBuilderFactory;

    /**
     * @param string $presetName name of the preset to use
     * @return string the rendered form head
     */
    public function render($presetName = 'default') {
        $content = '';
        $presetConfiguration = $this->formBuilderFactory->getPresetConfiguration($presetName);
        $stylesheets = isset($presetConfiguration['stylesheets']) ? $presetConfiguration['stylesheets'] : array();
        foreach ($stylesheets as $stylesheet) {
            $content .= sprintf('<link href="%s" rel="stylesheet">', $this->resolveResourcePath($stylesheet['source']));
        }
        $javaScripts = isset($presetConfiguration['javaScripts']) ? $presetConfiguration['javaScripts'] : array();
        foreach ($javaScripts as $javaScript) {
            $content .= sprintf('<script src="%s"></script>', $this->resolveResourcePath($javaScript['source']));
        }
        return $content;
    }

    /**
     * @param string $resourcePath
     * @return string
     */
    protected function resolveResourcePath($resourcePath) {
        // TODO: This method should be somewhere in the resource manager probably?
        $matches = array();
        preg_match('#resource://([^/]*)/Public/(.*)#', $resourcePath, $matches);
        if ($matches === array()) {
            throw new \TYPO3\Fluid\Core\ViewHelper\Exception('Resource path "' . $resourcePath . '" can\'t be resolved.', 1328543327);
        }
        $package = $matches[1];
        $path = $matches[2];
        return $this->resourcePublisher->getStaticResourcesWebBaseUri() . 'Packages/' . $package . '/' . $path;
    }
}
?>

$stylesheet['source'] and $javaScript['source'] contains paths like "resource://SomePackage/foo/bar".

If I see it correctly a similar code is used in the uri.resource ViewHelper but there should be some convenience method in the resource manager/publisher probably don't you think?

#3

Updated by Karsten Dambekalns almost 9 years ago

  • Status changed from Needs Feedback to Accepted
#4

Updated by Karsten Dambekalns over 8 years ago

  • Target version set to 2.0 beta 1
#5

Updated by Marc Neuhaus over 8 years ago

Another use-case:
i'm writing a Surf Deployment to push a file from inside a package to the remove node and need a proper way to have a real path to a resource file to use it with a shell command :)

#6

Updated by Karsten Dambekalns about 8 years ago

  • Target version changed from 2.0 beta 1 to 2.1
#7

Updated by Robert Lemke over 7 years ago

  • Target version deleted (2.1)
#8

Updated by Christian Müller almost 7 years ago

I guess this won't be implemented as API method. We can reconsider after the new resource mgm is merged. It could be a non API public method in the local file system implementation.

Also available in: Atom PDF