public function testInvoke() { $path = '/image.jpg'; $packageName = null; $this->packages->expects($this->once())->method('getUrl')->with($path, $packageName); $this->viewHelper->__invoke($path, $packageName); }
/** * Returns the public url/path of an asset. * * @param string $path * * @return string */ public function handle($path) { // getFile first to split mount point from path, if present. $file = $this->filesystem->getFile($path); if ($this->packages instanceof Packages) { return $this->packages->getUrl($file->getPath(), $file->getMountPoint()); } return $this->packages->getUrl($file->getPath()); }
public function render_file($filename, $extension) { $img = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'pjpeg']; $path = $this->uploads_directory_name . '/' . $filename; if (in_array($extension, $img)) { return '<img src="' . $this->packages->getUrl($path) . '" height="42">'; } return '<a href="' . $this->packages->getUrl($path) . '" download><i class="fa fa-download"></i></a>'; }
public function initLogger() { if (false === $this->isEnabled) { return ''; } $backendUrl = addslashes($this->router->generate(self::ROUTE)); $scriptUrl = $this->packages->getUrl(self::ASSET_PATH); $allowedLevels = empty($this->allowedLevels) ? '' : ' data-allowed-levels="' . implode(',', $this->allowedLevels) . '"'; return '<script src="' . $scriptUrl . '" data-backend-url="' . $backendUrl . '"' . $allowedLevels . '></script>'; }
/** * WebComponentService constructor. * @param ContainerInterface $container * @param Packages $packages * @param string $pathLib * @param string $pathComponents * @param array $componentArray */ public function __construct(ContainerInterface $container, Packages $packages, $pathLib = "", $pathComponents = "", array $componentArray) { $request = $container->get('request_stack'); $request = $request->getCurrentRequest(); $scheme = $request->getScheme(); $httpHost = $request->getHttpHost(); $asset = $packages->getUrl($pathComponents); $this->pathLib = $packages->getUrl($pathLib); $this->pathComponents = $scheme . '://' . $httpHost . $asset; $this->componentArray = $componentArray; }
/** * Add a file asset to the queue. * * @param FileAssetInterface $asset * * @throws \InvalidArgumentException */ public function add(FileAssetInterface $asset) { $url = $this->packages->getUrl($asset->getPath(), $asset->getPackageName()); $asset->setUrl($url); if ($asset->getType() === 'javascript') { $this->javascript[$url] = $asset; } elseif ($asset->getType() === 'stylesheet') { $this->stylesheet[$url] = $asset; } else { throw new \InvalidArgumentException(sprintf('Requested asset type %s is not valid.', $asset->getType())); } }
/** * @param $filename * * @return string */ public function visualizar_archivo($filename = null) { if ($filename) { $pathFile = $this->uploads_directory_name . '/' . $filename; $file = new File($pathFile); $img = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'pjpeg']; if (in_array($file->getExtension(), $img)) { return '<img src="' . $this->packages->getUrl($pathFile) . '" height="60">'; } return '<a href="' . $this->packages->getUrl($pathFile) . '" download>Descargar archivo</a>'; } }
/** * Add base snippets to the response. */ protected function addSnippets() { $generatorSnippet = (new Snippet())->setLocation(Target::END_OF_HEAD)->setCallback('<meta name="generator" content="Bolt">'); $this->queue->add($generatorSnippet); $canonicalUrl = $this->canonical->getUrl(); $canonicalSnippet = (new Snippet())->setLocation(Target::END_OF_HEAD)->setCallback($this->encode('<link rel="canonical" href="%s">', $canonicalUrl)); $this->queue->add($canonicalSnippet); if ($favicon = $this->config->get('general/favicon')) { $faviconUrl = $this->packages->getUrl($favicon, 'theme'); $faviconSnippet = (new Snippet())->setLocation(Target::END_OF_HEAD)->setCallback($this->encode('<link rel="shortcut icon" href="%s">', $faviconUrl)); $this->queue->add($faviconSnippet); } }
/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $definitions = $this->widgetDefinitionsRegistry->getWidgetDefinitions(); if ($definitions->isEmpty()) { return; } $definitions = $definitions->toArray(); foreach ($definitions as &$definition) { if (!empty($definition['icon'])) { $definition['icon'] = $this->assetHelper->getUrl($definition['icon']); } } $this->widgetDefinitionsRegistry->setWidgetDefinitions($definitions); }
/** * Get files from a specified source data * * @param array $source * @return array */ public function getFiles($source) { $resources = []; if (isset($source['path'], $source['url'])) { $finder = new Finder(); $pathParts = explode('/', $source['path']); $fileNamePattern = array_pop($pathParts); $files = $finder->name($fileNamePattern)->in(implode(DIRECTORY_SEPARATOR, $pathParts)); /** @var \SplFileInfo[] $files */ foreach ($files as $file) { $resources[$file->getFilename()] = $this->assetHelper->getUrl(rtrim($source['url'], '/') . '/' . $file->getFilename()); } } return $resources; }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $defaultWysiwygOptions = ['plugins' => ['textcolor', 'code', 'link', 'image', 'table', 'hr', 'preview', 'media'], 'toolbar_type' => self::TOOLBAR_FULL, 'skin_url' => 'bundles/oroform/css/tinymce', 'valid_elements' => implode(',', $this->htmlTagProvider->getAllowedElements()), 'menubar' => false, 'statusbar' => false, 'relative_urls' => false, 'remove_script_host' => false, 'convert_urls' => true, 'width' => 950, 'height' => 550]; $defaults = ['random_id' => true, 'wysiwyg_enabled' => (bool) $this->configManager->get('oro_form.wysiwyg_enabled'), 'wysiwyg_options' => $defaultWysiwygOptions, 'page-component' => ['module' => 'oroui/js/app/components/view-component', 'options' => ['view' => 'oroform/js/app/views/wysiwig-editor/wysiwyg-editor-view', 'content_css' => 'bundles/oroform/css/wysiwyg-editor.css']]]; $resolver->setDefaults($defaults); $resolver->setNormalizers(['wysiwyg_options' => function (Options $options, $wysiwygOptions) use($defaultWysiwygOptions) { if (empty($wysiwygOptions['toolbar_type']) || !array_key_exists($wysiwygOptions['toolbar_type'], $this->toolbars)) { $toolbarType = self::TOOLBAR_DEFAULT; } else { $toolbarType = $wysiwygOptions['toolbar_type']; } $wysiwygOptions['toolbar'] = $this->toolbars[$toolbarType]; $wysiwygOptions = array_merge($defaultWysiwygOptions, $wysiwygOptions); unset($wysiwygOptions['toolbar_type']); return $wysiwygOptions; }, 'attr' => function (Options $options, $attr) { $pageComponent = $options->get('page-component'); $wysiwygOptions = (array) $options->get('wysiwyg_options'); if ($this->assetHelper) { if (!empty($pageComponent['options']['content_css'])) { $pageComponent['options']['content_css'] = $this->assetHelper->getUrl($pageComponent['options']['content_css']); } if (!empty($wysiwygOptions['skin_url'])) { $wysiwygOptions['skin_url'] = $this->assetHelper->getUrl($wysiwygOptions['skin_url']); } } $pageComponent['options'] = array_merge($pageComponent['options'], $wysiwygOptions); $pageComponent['options']['enabled'] = (bool) $options->get('wysiwyg_enabled'); $attr['data-page-component-module'] = $pageComponent['module']; $attr['data-page-component-options'] = json_encode($pageComponent['options']); return $attr; }]); }
/** * Returns a list of choice attributes for the given integration type * * @param string $typeName * * @return array */ protected function getChoiceAttributes($typeName) { $attributes = []; $data = $this->itemsCache[$typeName]; if (!empty($data['icon'])) { $attributes['data-icon'] = $this->assetHelper->getUrl($data['icon']); } return $attributes; }
/** * @return Packages */ private function getPackages() { $version = $this->config['version']; $versionFormat = $this->config['version_format']; $baseUrl = $this->config['base_urls']; $basePath = $this->config['base_path']; $packages = new Packages(); $defaultPackage = $this->getPackageFactory()->createService($version, $versionFormat, $basePath, $baseUrl); $packages->setDefaultPackage($defaultPackage); foreach ((array) $this->config['packages'] as $name => $packageConfig) { $version = $packageConfig['version']; $versionFormat = $packageConfig['version_format']; $baseUrl = $packageConfig['base_urls']; $basePath = $packageConfig['base_path']; $package = $this->getPackageFactory()->createService($version, $versionFormat, $basePath, $baseUrl); $packages->addPackage($name, $package); } return $packages; }
/** * Generate an url in both Symfony 2 and Symfony 3 compatible ways. * * @param string $url * * @return string */ private function generateUrl($url) { if ($this->packages) { return $this->packages->getUrl($url); } if ($this->coreAssetsHelper) { return $this->coreAssetsHelper->getUrl($url); } return $url; }
/** * @param mixed[] $reference * @param AssetInterface $asset * * @return string */ public function rewrite(array $reference, AssetInterface $asset) { if (!$this->isRewritable($reference['url'], $asset->getSourcePath(), $asset->getTargetPath())) { return $reference[0]; } $absoluteSourcePath = $this->getAssetPath(dirname($this->createAssetPath($asset->getSourceRoot(), $asset->getSourcePath())), parse_url($reference['url'])['path']); $webTargetPath = $this->createAssetPath($this->rewriteDirectory, $this->namer->name($absoluteSourcePath, $asset)); $absoluteTargetPath = $this->webDirectory . $webTargetPath; $this->filesystem->{$this->strategy}($absoluteSourcePath, $absoluteTargetPath, true); return str_replace($reference['url'], $this->packages->getUrl($webTargetPath), $reference[0]); }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $assetsVersionBaseUrl = ''; $assetsVersionFormatted = ''; if ($this->assetHelper) { /** * As we can't get "assets_version_format" parameter and method "getVersion" returns only version value * without any formatting - we have to calculate formatted version url's parameter to be used inside * WYSIWYG editor. */ $assetsVersionBaseUrl = $this->assetHelper->getUrl('/'); $assetsVersionFormatted = substr($assetsVersionBaseUrl, strrpos($assetsVersionBaseUrl, '/') + 1); } $defaultWysiwygOptions = ['plugins' => self::$defaultPlugins, 'toolbar_type' => self::TOOLBAR_DEFAULT, 'skin_url' => 'bundles/oroform/css/tinymce', 'valid_elements' => implode(',', $this->htmlTagProvider->getAllowedElements()), 'menubar' => false, 'statusbar' => false, 'relative_urls' => false, 'remove_script_host' => false, 'convert_urls' => true, 'cache_suffix' => $assetsVersionFormatted, 'document_base_url' => $assetsVersionBaseUrl]; $defaults = ['wysiwyg_enabled' => (bool) $this->configManager->get('oro_form.wysiwyg_enabled'), 'wysiwyg_options' => $defaultWysiwygOptions, 'page-component' => ['module' => 'oroui/js/app/components/view-component', 'options' => ['view' => 'oroform/js/app/views/wysiwig-editor/wysiwyg-editor-view', 'content_css' => 'bundles/oroform/css/wysiwyg-editor.css']]]; $resolver->setDefaults($defaults); $resolver->setNormalizers(['wysiwyg_options' => function (Options $options, $wysiwygOptions) use($defaultWysiwygOptions) { if (!empty($wysiwygOptions['toolbar'])) { $wysiwygOptions = array_merge($defaultWysiwygOptions, $wysiwygOptions); unset($wysiwygOptions['toolbar_type']); return $wysiwygOptions; } if (empty($wysiwygOptions['toolbar_type']) || !array_key_exists($wysiwygOptions['toolbar_type'], self::$toolbars)) { $toolbarType = self::TOOLBAR_DEFAULT; } else { $toolbarType = $wysiwygOptions['toolbar_type']; } $wysiwygOptions['toolbar'] = self::$toolbars[$toolbarType]; $wysiwygOptions = array_merge($defaultWysiwygOptions, $wysiwygOptions); unset($wysiwygOptions['toolbar_type']); return $wysiwygOptions; }, 'attr' => function (Options $options, $attr) { $pageComponent = $options->get('page-component'); $wysiwygOptions = (array) $options->get('wysiwyg_options'); $pageComponent['options'] = array_merge($pageComponent['options'], $wysiwygOptions); $pageComponent['options']['enabled'] = (bool) $options->get('wysiwyg_enabled'); $attr['data-page-component-module'] = $pageComponent['module']; $attr['data-page-component-options'] = json_encode($pageComponent['options']); return $attr; }]); }
public function testAddErrorForInvalidPackageNameType() { $context = ['path' => 'test', 'package' => 456]; $options = [new PropertyPath('path'), new PropertyPath('package')]; $this->function->initialize($options); $message = 'Error message.'; $this->function->setMessage($message); $errors = new ArrayCollection(); $this->packages->expects($this->never())->method('getUrl'); $this->assertSame($context['path'], $this->function->evaluate($context, $errors)); $this->assertCount(1, $errors); $this->assertEquals(['message' => $message, 'parameters' => ['{{ path }}' => 'test', '{{ packageName }}' => 456, '{{ reason }}' => 'Expected null or a string value for the package name, got "integer".']], $errors->get(0)); }
public function testRewrite() { $asset = $this->createAssetMock(); $asset->expects($this->once())->method('getSourceRoot')->will($this->returnValue($sourceRoot = 'source_root')); $asset->expects($this->exactly(2))->method('getSourcePath')->will($this->returnValue($sourcePath = 'source_path')); $asset->expects($this->once())->method('getTargetPath')->will($this->returnValue($targetPath = 'target_path')); $reference = $this->createReference($path = 'foo.jpg'); $this->filesystem->expects($this->once())->method('exists')->with($this->identicalTo($absoluteSourcePath = $sourceRoot . DIRECTORY_SEPARATOR . $path))->will($this->returnValue(true)); $this->namer->expects($this->once())->method('name')->with($this->identicalTo($absoluteSourcePath))->will($this->returnValue($namedFile = 'named_file')); $webTargetPath = DIRECTORY_SEPARATOR . $this->rewriteDirectory . DIRECTORY_SEPARATOR . $namedFile; $this->filesystem->expects($this->once())->method($this->strategy)->with($this->identicalTo($absoluteSourcePath), $this->identicalTo($this->webDirectory . $webTargetPath), $this->isTrue()); $this->packages->expects($this->once())->method('getUrl')->with($this->identicalTo($webTargetPath))->will($this->returnValue($packagedPath = 'packaged_path')); $this->assertSame('url("' . $packagedPath . '")', $this->cssRewriter->rewrite($reference, $asset)); }
/** * {@inheritdoc} */ protected function doEvaluate($context) { $path = $this->resolveValue($context, $this->path); if ($path === null) { return $path; } if (!is_string($path)) { $this->addError($context, sprintf('Expected a string value for the path, got "%s".', is_object($path) ? get_class($path) : gettype($path))); return $path; } $packageName = $this->resolveValue($context, $this->packageName); if ($packageName !== null && !is_string($packageName)) { $this->addError($context, sprintf('Expected null or a string value for the package name, got "%s".', is_object($packageName) ? get_class($packageName) : gettype($packageName))); return $path; } return $this->packages->getUrl($this->normalizeAssetsPath($path), $packageName); }
public function register(Application $app) { $app['asset.packages'] = $app->share(function ($app) { $defaultPackage = new Package($app['asset.version_strategy']('view')); $packages = new Packages($defaultPackage); $packages->addPackage('bolt', $app['asset.package_factory']('view')); $packages->addPackage('extensions', new PathPackage('', $app['asset.version_strategy']('web'), $app['asset.context'])); $packages->addPackage('files', $app['asset.package_factory']('files')); $packages->addPackage('theme', $app['asset.package_factory']('theme')); return $packages; }); $app['asset.package_factory'] = $app->protect(function ($name) use($app) { return new PathPackage($app['resources']->getUrl($name), $app['asset.version_strategy']($name), $app['asset.context']); }); $app['asset.version_strategy'] = $app->protect(function ($name) use($app) { return new Asset\BoltVersionStrategy($app['filesystem']->getFilesystem($name), $app['asset.salt']); }); $app['asset.context'] = $app->share(function () use($app) { return new RequestStackContext($app['request_stack']); }); $app['asset.salt.factory'] = function () use($app) { return $app['randomgenerator']->generateString(10); }; $app['asset.salt'] = $app->share(function ($app) { $file = $app['filesystem']->getFile('cache://.assetsalt'); try { $salt = $file->read(); } catch (FileNotFoundException $e) { $salt = $app['asset.salt.factory']; $file->put($salt); } return $salt; }); $app['asset.injector'] = $app->share(function () { $snippets = new Asset\Injector(); return $snippets; }); $app['asset.queue.file'] = $app->share(function ($app) { $queue = new Asset\File\Queue($app['asset.injector'], $app['asset.packages']); return $queue; }); $app['asset.queue.snippet'] = $app->share(function ($app) { $queue = new Asset\Snippet\Queue($app['asset.injector'], $app['cache'], $app['config'], $app['resources']); return $queue; }); $app['asset.queue.widget'] = $app->share(function ($app) { $queue = new Asset\Widget\Queue($app['asset.injector'], $app['cache'], $app['render']); return $queue; }); $app['asset.queues'] = $app->share(function ($app) { return [$app['asset.queue.file'], $app['asset.queue.snippet'], $app['asset.queue.widget']]; }); }
public function register(Application $app) { $app['asset.packages'] = $app->share(function ($app) { $packages = new Packages(); $packages->addPackage('bolt', $app['asset.package.bolt']); $packages->addPackage('extensions', new PathPackage('', $app['asset.version_strategy']('web'), $app['asset.context'])); $packages->addPackage('files', $app['asset.package_factory']('files')); $packages->addPackage('theme', $app['asset.package_factory']('theme')); $packages->addPackage('themes', $app['asset.package_factory']('themes')); return $packages; }); $app['asset.package.bolt'] = $app->share(function ($app) { /* * This is technically the wrong directory as our composer script handler * copies the assets to the project's web directory. But since this is * just to check the file's last modified time for versioning it will do fine. */ $boltViewDir = $app['filesystem']->getDir('bolt://app/view'); /* * Remove app/view from path as AssetUrl plugin will include it. * This is because "bolt" FS points to bolt's root dir, but * "bolt" asset package points to "bolt_root_dir/app/view". * * This works with composer installs as well. */ return new Asset\UnprefixedPathPackage($boltViewDir->getPath() . '/', $app['resources']->getUrl('view', false), $app['asset.version_strategy']($boltViewDir), $app['asset.context']); }); $app['asset.package_factory'] = $app->protect(function ($name) use($app) { return new PathPackage($app['resources']->getUrl($name, false), $app['asset.version_strategy']($name), $app['asset.context']); }); $app['asset.version_strategy'] = $app->protect(function ($nameOrDir) use($app) { $dir = $nameOrDir instanceof DirectoryInterface ? $nameOrDir : $app['filesystem']->getFilesystem($nameOrDir)->getDir(''); return new Asset\BoltVersionStrategy($dir, $app['asset.salt']); }); $app['asset.context'] = $app->share(function () use($app) { return new RequestStackContext($app['request_stack']); }); $app['asset.salt.factory'] = function () use($app) { return $app['randomgenerator']->generateString(10); }; $app['asset.salt'] = $app->share(function ($app) { $file = $app['filesystem']->getFile('cache://.assetsalt'); try { $salt = $file->read(); } catch (FileNotFoundException $e) { $salt = $app['asset.salt.factory']; $file->put($salt); } return $salt; }); $app['asset.injector'] = $app->share(function () { $snippets = new Asset\Injector(); return $snippets; }); $app['asset.queue.file'] = $app->share(function ($app) { $queue = new Asset\File\Queue($app['asset.injector'], $app['asset.packages']); return $queue; }); $app['asset.queue.snippet'] = $app->share(function ($app) { $queue = new Asset\Snippet\Queue($app['asset.injector'], $app['cache'], $app['config'], $app['resources']); return $queue; }); $app['asset.queue.widget'] = $app->share(function ($app) { $queue = new Asset\Widget\Queue($app['asset.injector'], $app['cache'], $app['twig']); return $queue; }); $app['asset.queues'] = $app->share(function ($app) { return [$app['asset.queue.file'], $app['asset.queue.snippet'], $app['asset.queue.widget']]; }); }
/** * @expectedException InvalidArgumentException */ public function testUndefinedPackage() { $packages = new Packages(); $packages->getPackage('a'); }
* Created by PhpStorm. * User: dima * Date: 15.09.15 * Time: 20:40 */ namespace { require_once __DIR__ . '/vendor/autoload.php'; use Symfony\Component\Asset\Context\RequestStackContext; use Symfony\Component\Asset\Packages; use Symfony\Component\Asset\PathPackage; use Symfony\Component\Asset\UrlPackage; use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy; use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; $requestStack = new RequestStack(); $requestStack->push(Request::createFromGlobals()); $requestStackContext = new RequestStackContext($requestStack); $jsAssetsPackage = new PathPackage('/asset/js/', new StaticVersionStrategy('v1', '%1$s?%2$s'), $requestStackContext); $cssAssetsPackage = new PathPackage('/asset/css/', new StaticVersionStrategy('v1', '%1$s?%2$s'), $requestStackContext); $imgAssetsPackage = new PathPackage('/asset/img/', new StaticVersionStrategy('v1', '%1$s?%2$s'), $requestStackContext); $jqueryPackage = new UrlPackage(['https://code.jquery.com/', 'http://code.jquery.com/'], new EmptyVersionStrategy(), $requestStackContext); $namedPackages = ['js' => $jsAssetsPackage, 'jquery-cdn' => $jqueryPackage, 'css' => $cssAssetsPackage, 'img' => $imgAssetsPackage]; $packages = new Packages(null, $namedPackages); echo '<pre>'; echo $packages->getUrl('react.js', 'js'), "\n"; echo $packages->getUrl('jquery-2.1.4.min.js', 'jquery-cdn'), "\n"; echo $packages->getUrl('style.css', 'css'), "\n"; echo $packages->getUrl('image.png', 'img'), "\n\n\n"; require_once __DIR__ . '/loadersTest.php'; }
/** * Returns the version of the assets in a package * * @return string */ public function getAssetsVersion(array $parameters = array(), \Smarty_Internal_Template $template) { $parameters = array_merge(array('package' => null), $parameters); return $this->packages->getVersion(null, $parameters['package']); }
/** * Render the locale path. * * @param $basePath * @param $locale * * @return string */ public function renderLocalePath($basePath, $locale) { $localePath = sprintf('%s/js/locales/bootstrap-datetimepicker.%s.js', $basePath, $locale); return $this->packages->getUrl($localePath); }
/** * @param $path * * @return string */ public function getBasePath($path) { return $this->fixPath($this->assetsHelper->getUrl($path)); }
public function testGetJsPath() { $this->assetsHelperMock->expects($this->once())->method('getUrl')->with($this->equalTo('foo'))->will($this->returnValue('bar')); $this->assertSame('bar', $this->helper->getJsPath('foo')); }
/** * {@inheritdoc} */ public function getUrl($path, $packageName = null) { return $this->assetPackages->getUrl($path, $packageName); }