Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial inline js routing functionality #23

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions DependencyInjection/Compiler/MountFunctionCompilersPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TwigJsBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* Mount Function Compiler Pass
*
* This compiler pass finds services which are tagged as function compilers and
* adds them to the twig js compiler.
*
* @author Josiah <[email protected]>
* @author Johannes M. Schmitt <[email protected]>
*/
class MountFunctionCompilersPass implements CompilerPassInterface
{
/** {@inheritdoc} */
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('twig_js.compiler')) {
return;
}

$compiler = $container->getDefinition('twig_js.compiler');

// Find the tagged function compilers and add them to the javascript
// compiler
foreach ($container->findTaggedServiceIds('twig_js.function_compiler')
as $id => $tags) {
foreach ($tags as $tag) {
if (!array_key_exists('function', $tag)) {
throw new \LogicException("When defining a TwigJS function complier you must specify the function name in the service tags 'function' attribute.");
}

// Function parameters
$params = array($tag['function'], new Reference($id));

// Add the priority if it has been set on the tag
if (array_key_exists('priority', $tag)) {
$params[] = (int) $tag['priority'];
}

// Add the function compiler dependancy
$compiler->addMethodCall('addFunctionCompiler', $params);
}
}
}
}
22 changes: 22 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('twig_js');

$rootNode
->children()
->arrayNode('route_compilation')
->info("Enables inline route compilation")
->treatFalseLike(array('url' => false, 'path' => false))
->treatTrueLike(array('url' => true, 'path' => true))
->treatNullLike(array('url' => true, 'path' => true))
->addDefaultsIfNotSet()
->children()
->booleanNode('url')
->defaultFalse()
->treatNullLike(false)
->end()
->booleanNode('path')
->defaultFalse()
->treatNullLike(false)
->end()
->end()
->end()
->end()
;

return $treeBuilder;
}
}
25 changes: 25 additions & 0 deletions DependencyInjection/JMSTwigJsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$loader->load('filters.xml');
$loader->load('functions.xml');

// Load the route compilation if it is defined
if (array_key_exists('route_compilation', $config)) {
$this->loadRouteCompilationConfig($config['route_compilation'], $container);
}
}

/**
* Configure the service container for route compilation
*
* @param array $config Route compilation configuration
* @param ContainerBuilder $container Service container
*/
protected function loadRouteCompilationConfig(array $config, ContainerBuilder $container)
{
$routingCompiler = $container->getDefinition('twig_js.functions.routing_compiler');

if ($config['path']) {
$routingCompiler->addTag('twig_js.function_compiler', array('function' => 'path'));
}

if ($config['url']) {
$routingCompiler->addTag('twig_js.function_compiler', array('function' => 'url'));
}
}

public function getAlias()
Expand Down
2 changes: 2 additions & 0 deletions JMSTwigJsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace JMS\TwigJsBundle;

use JMS\TwigJsBundle\DependencyInjection\Compiler\MountFilterCompilersPass;
use JMS\TwigJsBundle\DependencyInjection\Compiler\MountFunctionCompilersPass;
use JMS\TwigJsBundle\DependencyInjection\JMSTwigJsExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -33,5 +34,6 @@ public function getContainerExtension()
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new MountFilterCompilersPass());
$container->addCompilerPass(new MountFunctionCompilersPass());
}
}
16 changes: 16 additions & 0 deletions Resources/config/functions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="twig_js.function.routing_compiler.class">JMS\TwigJsBundle\TwigJs\Compiler\RoutingFunctionCompiler</parameter>
</parameters>

<services>
<service id="twig_js.functions.routing_compiler" class="%twig_js.function.routing_compiler.class%" public="false">
<argument type="service" id="router" />
</service>
</services>
</container>
8 changes: 8 additions & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ to specify all of them.

See twig.js_ documentation for how to render your templates.

Router Integration
~~~~~~~~~~~~~~~~~~

You can integrate the symfony2 router into twig.js_ using built in features
or other libraries. See `Routing Integration`_ for more information.

.. _`Routing Integration`: routing.rst

Custom Integration
~~~~~~~~~~~~~~~~~~
If you need to customize your integration, this bundle makes a service named
Expand Down
29 changes: 29 additions & 0 deletions Resources/doc/routing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
===================
Routing Integration
===================

Routing information from your symfony application can be integrated your
compiled javascript templates either through inline route compilation or by
integrating an javascript routing solution (such as the FOSJsRoutingBundle_).

.. _FOSJsRoutingBundle: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

Inline Route Compilation
========================

The easiest method of integration routing information is to take advantage of
the built in inline route compilation functionality. This funcionality
interprets a route and produces a javascript function which will generate a url
the same as symfony would.

How to enable this feature
--------------------------

Add the following lines to your application configuration:

::

# app/config/config.yml
twig_js:

route_compilation: true
Loading