Skip to content

Commit

Permalink
Merge pull request #6 from skroczek/dependencies-update
Browse files Browse the repository at this point in the history
Dependencies update
  • Loading branch information
schmittjoh authored Sep 12, 2016
2 parents 6f23385 + d6963e7 commit 725da1b
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},

"require-dev": {
"symfony/routing": "2.1.*",
"symfony/routing": "~2.1||~3.0",
"doctrine/common": "2.*",
"symfony/yaml": "~2.0 || ~3.0"
},
Expand Down
90 changes: 56 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Object Routing Library
======================

This library makes generating routes for objects a breeze, and is not tied to any concrete router implementation. As
part of the library, we ship an adapter for Symfony 2.1's router.
part of the library, we ship an adapter for Symfony's router. For Symfony router version <2.2 use
``JMS/ObjectRouting/Symfony/Symfony21Adapter`` and for a version >=2.2 use ``JMS/ObjectRouting/Symfony/Symfony22Adapter``.

Installation
------------
Expand Down Expand Up @@ -106,6 +107,10 @@ For Twig, this library also provides two new functions:
{# equivalent to #}
{{ url('the-actual-route-name', {'slug': blogPost.slug}) }}
For compatibility reason this library is shipped with two Twig extensions. If you are using Twig 1.*
``JMS/ObjectRouting/Twig/RoutingExtension`` will fit your needs and if you need support for Twig 2.* you can use
``JMS/ObjectRouting/Twig/Routing20Extension``.

License
-------

Expand Down
42 changes: 42 additions & 0 deletions src/JMS/ObjectRouting/Symfony/Symfony22Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* Copyright 2016 Sebastian Kroczek <[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\ObjectRouting\Symfony;

use JMS\ObjectRouting\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Class Symfony22Adapter
* @package JMS\ObjectRouting\Symfony
* @author Sebastian Kroczek <[email protected]>
*/
class Symfony22Adapter implements RouterInterface
{
private $delegate;

public function __construct(\Symfony\Component\Routing\RouterInterface $router)
{
$this->delegate = $router;
}

public function generate($name, array $params, $absolute = false)
{
return $this->delegate->generate($name, $params, $absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH);
}
}
59 changes: 59 additions & 0 deletions src/JMS/ObjectRouting/Twig/Routing20Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* Copyright 2016 Sebastian Kroczek <[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\ObjectRouting\Twig;

use JMS\ObjectRouting\ObjectRouter;

/**
* Class Routing20Extension
* @package JMS\ObjectRouting\Twig
* @author Sebastian Kroczek <[email protected]>
*/
class Routing20Extension extends \Twig_Extension
{
private $router;

public function __construct(ObjectRouter $router)
{
$this->router = $router;
}

public function getFunctions()
{
return array(
new \Twig_SimpleFunction('object_path', array($this, 'path')),
new \Twig_SimpleFunction('object_url', array($this, 'url')),
);
}

public function url($type, $object, array $extraParams = array())
{
return $this->router->generate($type, $object, true, $extraParams);
}

public function path($type, $object, array $extraParams = array())
{
return $this->router->generate($type, $object, false, $extraParams);
}

public function getName()
{
return 'jms.object_routing';
}
}
30 changes: 30 additions & 0 deletions tests/JMS/Tests/ObjectRouting/Symfony/Symfony22AdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace JMS\Tests\ObjectRouting\Symfony;

use JMS\ObjectRouting\Symfony\Symfony22Adapter;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class Symfony22AdapterTest extends \PHPUnit_Framework_TestCase
{
/** @var Symfony22Adapter */
private $adapter;
private $router;

public function testGenerate()
{
$this->router->expects($this->once())
->method('generate')
->with('foo', array('bar' => 'baz'), UrlGeneratorInterface::ABSOLUTE_URL)
->will($this->returnValue('/foo-bar-baz'));

$this->assertEquals('/foo-bar-baz', $this->adapter->generate('foo', array('bar' => 'baz'), true));
}

protected function setUp()
{
$this->adapter = new Symfony22Adapter(
$this->router = $this->getMock('Symfony\Component\Routing\RouterInterface')
);
}
}

0 comments on commit 725da1b

Please sign in to comment.