Permalink
Browse files

Allow exposing files in addition to directories

  • Loading branch information...
1 parent 9aada87 commit 735880db8758e0920064eeeffea773c3de0b2858 @Seldaek Seldaek committed Jun 1, 2012
@@ -55,10 +55,11 @@ public function __construct($basePath)
* Register a directory-to-namespace mapping.
* @param string $path
* @param string $namespace
+ * @param Boolean $isDir true if the path is a directory
*/
- public function registerNamespace($path, $namespace)
+ public function registerNamespace($path, $namespace, $isDir = true)
{
- $this->namespaces[realpath($path)] = $namespace;
+ $this->namespaces[realpath($path)] = array('namespace' => $namespace, 'is_dir' => $isDir);
}
/**
@@ -67,9 +68,13 @@ public function registerNamespace($path, $namespace)
public function getModulePath($filename)
{
$filename = realpath($filename);
- foreach ($this->namespaces as $path => $namespace) {
+ foreach ($this->namespaces as $path => $settings) {
if (strpos($filename, $path) === 0) {
- return preg_replace('#[/\\\\]+#', '/', $this->basePath . '/' . $namespace . '/' . substr($filename, strlen($path)));
+ if ($settings['is_dir']) {
+ return preg_replace('#[/\\\\]+#', '/', $this->basePath . '/' . $settings['namespace'] . '/' . substr($filename, strlen($path)));
+ }
+
+ return preg_replace('#[/\\\\]+#', '/', $this->basePath . '/' . $settings['namespace'] . '.' . pathinfo($filename, PATHINFO_EXTENSION));
}
}
return false;
@@ -3,22 +3,22 @@
/**
* Copyright (c) 2011 Hearsay News Products, Inc.
*
- * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
- * The above copyright notice and this permission notice shall be included in
+ * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
@@ -52,7 +52,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('hearsay_require_js.base_directory', $this->getRealPath($config['base_directory'], $container));
$container->setParameter('hearsay_require_js.require_js_src', $config['require_js_src']);
-
+
$container->setParameter('hearsay_require_js.initialize_template', $config['initialize_template']);
$hide_unoptimized_assets = false;
@@ -92,7 +92,7 @@ public function load(array $configs, ContainerBuilder $container)
* Configure a mapping from a filesystem path to a RequireJS namespace.
* @param string $location
* @param string $path
- * @param ContainerBuilder $container
+ * @param ContainerBuilder $container
* @param boolean $generateAssets
*/
protected function addNamespaceMapping($location, $path, ContainerBuilder $container, $generateAssets = true)
@@ -101,7 +101,7 @@ protected function addNamespaceMapping($location, $path, ContainerBuilder $conta
// Register the namespace with the configuration
$mapping = $container->getDefinition('hearsay_require_js.namespace_mapping');
- $mapping->addMethodCall('registerNamespace', array($location, $path));
+ $mapping->addMethodCall('registerNamespace', array($location, $path, is_dir($location)));
// And with the optimizer filter
if ($path && $container->hasDefinition('hearsay_require_js.optimizer_filter')) {
@@ -138,7 +138,7 @@ protected function addExternalNamespaceMapping($location, $path, ContainerBuilde
* Helper to convert bundle-notation paths to filesystem paths.
* @param string $path
* @param ContainerBuilder $container
- * @return string
+ * @return string
*/
private function getRealPath($path, ContainerBuilder $container)
{
@@ -56,6 +56,11 @@ public function getContent()
{
$finder = Finder::create();
$files = '';
+
+ if (is_file($this->path)) {
+ return strtr($this->path, '\\', '/');
+ }
+
foreach($finder->files()->in($this->path) as $file) {
$files .= strtr($file, '\\', '/') . "\n";
}
View
@@ -59,6 +59,8 @@ directory structure like::
- scripts/
- three/
- four.js
+ - libs/
+ - text.js
Your configuration might look something like::
@@ -69,6 +71,15 @@ Your configuration might look something like::
blog: %kernel.root_dir%/../src/Acme/BlogBundle/Resources/scripts
comment: '@AcmeCommentBundle/Resources/scripts'
+You can also expose files directly using the following syntax. In this
+example the file that otherwise would be exposed as `/js/libs/text.js`
+(according to the configuration above) will now be exposed as `/js/text.js`.
+
+ # app/config/config.yml
+ hearsay_require_js:
+ paths:
+ text: '@AcmeCommentBundle/Resources/scripts/libs/text.js'
+
This specifies base namespaces for each directory, so you would then reference
modules like::
@@ -3,22 +3,22 @@
/**
* Copyright (c) 2011 Hearsay News Products, Inc.
*
- * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
- * The above copyright notice and this permission notice shall be included in
+ * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
@@ -47,28 +47,28 @@ public function testNamespacesMapped()
);
$container = $this->getContainerBuilder();
$loader = new HearsayRequireJSExtension();
-
+
$loader->load(array($config), $container);
-
+
// Check the namespace mapping
$mapping = $container->getDefinition('hearsay_require_js.namespace_mapping');
$methods = $mapping->getMethodCalls();
$this->assertEquals(2, count($methods), 'Incorrect number of method calls on namespace mapping');
$this->assertContains(array(
- 'registerNamespace', array('/home/user/path', 'namespace'),
+ 'registerNamespace', array('/home/user/path', 'namespace', is_dir('/home/user/base')),
), $methods, 'Did not find expected method call');
$this->assertContains(array(
- 'registerNamespace', array('/home/user/base', ''),
+ 'registerNamespace', array('/home/user/base', '', is_dir('/home/user/base')),
), $methods, 'Did not find expected method call');
-
+
// Check the optimization filter
$filter = $container->getDefinition('hearsay_require_js.optimizer_filter');
$methods = $filter->getMethodCalls();
$this->assertEquals(1, count($methods), 'Incorrect number of method calls on optimizer filter');
$this->assertContains(array(
'setOption', array('paths.namespace', '/home/user/path'),
), $methods, 'Did not find expected method call');
-
+
// And the Assetic resources
foreach(array('/home/user/base', '/home/user/path') as $path) {
/* @var $resource \Symfony\Component\DependencyInjection\DefinitionDecorator */
@@ -96,15 +96,15 @@ public function testAssetsNotHiddenByDefault()
$container = $this->getContainerBuilder();
$loader = new HearsayRequireJSExtension();
-
+
$loader->load(array($config), $container);
-
+
// Make sure we have the relevant assetic resources
foreach(array('/home/user/base', '/home/user/path') as $path) {
$this->assertTrue($container->hasDefinition('hearsay_require_js.directory_filename_resource.' . md5($path)));
}
}
-
+
public function testAssetsCanBeHidden()
{
$config = array(
@@ -119,28 +119,28 @@ public function testAssetsCanBeHidden()
);
$container = $this->getContainerBuilder();
$loader = new HearsayRequireJSExtension();
-
+
$loader->load(array($config), $container);
-
+
// Make sure we don't have any assetic resources
foreach(array('/home/user/base', '/home/user/path') as $path) {
$this->assertFalse($container->hasDefinition('hearsay_require_js.directory_filename_resource.' . md5($path)));
}
}
-
+
public function testOptimizerOmittedIfNotConfigured()
{
$config = array(
'base_directory' => '/home/user/base',
);
$container = $this->getContainerBuilder();
$loader = new HearsayRequireJSExtension();
-
+
$loader->load(array($config), $container);
-
+
$this->assertFalse($container->hasDefinition('hearsay_require_js.optimizer_filter'), 'Expected optimizer filter not to be defined');
}
-
+
public function testOptimizerOptionsSet()
{
$config = array(
@@ -154,29 +154,29 @@ public function testOptimizerOptionsSet()
);
$container = $this->getContainerBuilder();
$loader = new HearsayRequireJSExtension();
-
+
$loader->load(array($config), $container);
-
+
$optimizer = $container->getDefinition('hearsay_require_js.optimizer_filter');
$methods = $optimizer->getMethodCalls();
$this->assertEquals(1, count($methods), 'Incorrect number of method calls on optimizer');
$this->assertContains(array(
'setOption', array('option', 'value'),
), $methods, 'Did not find expected method call');
}
-
+
public function testExceptionOnUnrecognizedBundle()
{
$config = array(
'base_directory' => '@UnknownBundle/Resources/scripts',
);
$container = $this->getContainerBuilder();
$loader = new HearsayRequireJSExtension();
-
+
$this->setExpectedException('InvalidArgumentException', 'Unrecognized bundle: "UnknownBundle"');
$loader->load(array($config), $container);
}
-
+
/**
* Helper to get a new container builder with necessary initialization
* performed.
@@ -187,9 +187,9 @@ protected function getContainerBuilder()
$container = new ContainerBuilder();
$bundles = array(
'HearsayRequireJSBundle' => 'Hearsay\RequireJSBundle\HearsayRequireJSBundle',
- );
+ );
$container->setParameter('kernel.bundles', $bundles);
return $container;
}
-
+
}

0 comments on commit 735880d

Please sign in to comment.