Skip to content
This repository has been archived by the owner on Jan 31, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Aug 12, 2013
0 parents commit 1bfb084
Show file tree
Hide file tree
Showing 23 changed files with 684 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

php:
- 5.3
- 5.4
- 5.5

env:
- SYMFONY_VERSION=2.0.*
- SYMFONY_VERSION=2.1.*
- SYMFONY_VERSION=2.2.*
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=dev-master

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar require symfony/framework-bundle:${SYMFONY_VERSION} --no-update
- php composer.phar update

script:
- phpunit --coverage-text
17 changes: 17 additions & 0 deletions BazingaRestExtraBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of the RestExtraBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Bazinga\Bundle\RestExtraBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class BazingaRestExtraBundle extends Bundle
{
}
30 changes: 30 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Contributing
============

First of all, **thank you** for contributing, **you are awesome**!

Here are a few rules to follow in order to ease code reviews, and discussions before
maintainers accept and merge your work.

You MUST follow the [PSR-1](http://www.php-fig.org/psr/1/) and
[PSR-2](http://www.php-fig.org/psr/2/). If you don't know about any of them, you
should really read the recommendations. Can't wait? Use the [PHP-CS-Fixer
tool](http://cs.sensiolabs.org/).

You MUST run the test suite.

You MUST write (or update) unit tests.

You SHOULD write documentation.

Please, write [commit messages that make
sense](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html),
and [rebase your branch](http://git-scm.com/book/en/Git-Branching-Rebasing)
before submitting your Pull Request.

One may ask you to [squash your
commits](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html)
too. This is used to "clean" your Pull Request before merging it (we don't want
commits such as `fix tests`, `fix 2`, `fix 3`, etc.).

Thank you!
32 changes: 32 additions & 0 deletions DependencyInjection/BazingaRestExtraExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file is part of the RestExtraBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Bazinga\Bundle\RestExtraBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;

/**
* @author William Durand <william.durand1@gmail.com>
*/
class BazingaRestExtraExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

if (!empty($config['link_request_listener'])) {
$loader->load('link_request_listener.xml');
}
}
}
39 changes: 39 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of the RestExtraBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Bazinga\Bundle\RestExtraBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* @author William Durand <william.durand1@gmail.com>
*/
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree builder.
*
* @return TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('bazinga_rest_extra');

$rootNode
->children()
->scalarNode('link_request_listener')->defaultFalse()->end()
->end()
;

return $treeBuilder;
}
}
116 changes: 116 additions & 0 deletions EventListener/LinkRequestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* This file is part of the RestExtraBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Bazinga\Bundle\RestExtraBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* @author William Durand <william.durand1@gmail.com>
* @author Samuel Gordalina <samuel.gordalina@gmail.com>

This comment has been minimized.

Copy link
@willdurand

willdurand Aug 14, 2013

Author Owner

poke @gordalina

This comment has been minimized.

Copy link
@gordalina

gordalina Aug 14, 2013

👍

*/
class LinkRequestListener
{
/**
* @var ControllerResolverInterface
*/
private $resolver;

/**
* @var UrlMatcherInterface
*/
private $urlMatcher;

/**
* @param ControllerResolverInterface $controllerResolver The 'controller_resolver' service
* @param UrlMatcherInterface $urlMatcher The 'router' service
*/
public function __construct(ControllerResolverInterface $controllerResolver, UrlMatcherInterface $urlMatcher)
{
$this->resolver = $controllerResolver;
$this->urlMatcher = $urlMatcher;
}

public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->getRequest()->headers->has('link')) {
return;
}

$links = array();
$header = $event->getRequest()->headers->get('link');

/*
* Due to limitations, multiple same-name headers are sent as comma
* separated values.
*
* This breaks those headers into Link headers following the format
* http://tools.ietf.org/html/rfc2068#section-19.6.2.4
*/
while (preg_match('/^((?:[^"]|"[^"]*")*?),/', $header, $matches)) {
$header = trim(substr($header, strlen($matches[0])));
$links[] = $matches[1];
}

if ($header) {
$links[] = $header;
}

$requestMethod = $this->urlMatcher->getContext()->getMethod();
// Force the GET method to avoid the use of the
// previous method (LINK/UNLINK)
$this->urlMatcher->getContext()->setMethod('GET');

// The controller resolver needs a request to resolve the controller.
$stubRequest = new Request();

foreach ($links as $idx => $link) {
$linkParams = explode(';', trim($link));
$resource = array_shift($linkParams);
$resource = preg_replace('/<|>/', '', $resource);

try {
$route = $this->urlMatcher->match($resource);
} catch (\Exception $e) {
// If we don't have a matching route we return
// the original Link header
continue;
}

$stubRequest->attributes->replace($route);

if (false === $controller = $this->resolver->getController($stubRequest)) {
continue;
}

$arguments = $this->resolver->getArguments($stubRequest, $controller);

try {
$result = call_user_func_array($controller, $arguments);

// By convention the controller action must return an array
if (!is_array($result)) {
continue;
}

// The key of first item is discarded
$links[$idx] = current($result);
} catch (\Exception $e) {
continue;
}
}

$event->getRequest()->attributes->set('links', $links);
$this->urlMatcher->getContext()->setMethod($requestMethod);
}
}
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
BazingaRestExtraBundle
======================

[![Build Status](https://secure.travis-ci.org/willdurand/BazingaRestExtraBundle.png)](http://travis-ci.org/willdurand/BazingaRestExtraBundle)


Documentation
-------------

For documentation, see:

Resources/doc/

[Read the documentation](https://github.com/willdurand/BazingaRestExtraBundle/blob/master/Resources/doc/index.md)


Contributing
------------

See CONTRIBUTING file.


Credits
-------

* William Durand <william.durand1@gmail.com>
* [All contributors](https://github.com/willdurand/BazingaRestExtraBundle/contributors)


License
-------

This bundle is released under the MIT license. See the complete license in the
bundle:

Resources/meta/LICENSE
17 changes: 17 additions & 0 deletions Resources/config/link_request_listener.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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="bazinga_rest_extra.event_listener.link_request.class">Bazinga\Bundle\RestExtraBundle\EventListener\LinkRequestListener</parameter>
</parameters>

<services>
<service id="bazinga_rest_extra.event_listener.link_request" class="%bazinga_rest_extra.event_listener.link_request.class%">
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
<argument type="service" id="controller_resolver" />
<argument type="service" id="router" />
</service>
</services>
</container>
38 changes: 38 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
BazingaRestExtraBundle
======================


Usage
-----


Installation
------------

Require `willdurand/rest-extra-bundle` to your `composer.json` file:

``` json
{
"require": {
"willdurand/rest-extra-bundle": "@stable"
}
}
```

Register the bundle in `app/AppKernel.php`:

// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Bazinga\Bundle\RestExtraBundle\BazingaRestExtraBundle(),
);
}

Enable the bundle's configuration in `app/config/config.yml`:

``` yaml
# app/config/config.yml
bazinga_rest_extra: ~
```
19 changes: 19 additions & 0 deletions Resources/meta/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2013 William Durand <william.durand1@gmail.com>

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 furnished
to do so, subject to the following conditions:

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 SOFTWARE.
Loading

0 comments on commit 1bfb084

Please sign in to comment.