Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wjzijderveld authored and fritsjanb committed Sep 2, 2014
0 parents commit 7477681
Show file tree
Hide file tree
Showing 23 changed files with 901 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
/vendor
/Tests/Functional/cache
/Tests/Functional/logs
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.4
- 5.5
- hhvm
- hhvm-nightly

matrix:
allow_failures:
- php: hhvm-nightly

install: composer install
40 changes: 40 additions & 0 deletions Context/UserContextFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Bundle\ToggleBundle\Context;

use Qandidate\Toggle\Context;
use Qandidate\Toggle\ContextFactory;
use Symfony\Component\Security\Core\SecurityContextInterface;

class UserContextFactory extends ContextFactory
{
public function __construct(SecurityContextInterface $securityContext)
{
$this->securityContext = $securityContext;
}

/**
* {@inheritDoc}
*/
public function createContext()
{
$context = new Context();

$token = $this->securityContext->getToken();

if (null !== $token) {
$context->set('username', $this->securityContext->getToken()->getUsername());
}

return $context;
}
}
47 changes: 47 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Bundle\ToggleBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();

$rootNode = $treeBuilder->root('qandidate_toggle');

$rootNode
->children()
->enumNode('persistence')
->values(array('in_memory', 'redis'))
->defaultValue('in_memory')
->end()
->scalarNode('context_factory')
->defaultNull()
->end()
->scalarNode('redis_namespace')
->defaultValue('toggle_%kernel.environment%')
->end()
->scalarNode('redis_client')
->defaultNull()
->end()
->end();

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

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Bundle\ToggleBundle\DependencyInjection;

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

class QandidateToggleExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/'));
$loader->load('services.xml');

$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(), $configs);

$collection = 'in_memory';
if ('redis' === $config['persistence']) {
$loader->load('redis.xml');

$collection = 'predis';

$container->setParameter('qandidate.toggle.redis.namespace', $config['redis_namespace']);
$container->setAlias('qandidate.toggle.redis.client', $config['redis_client']);
}

$container->setAlias('qandidate.toggle.collection', 'qandidate.toggle.collection.' . $collection);

$contextFactoryService = 'qandidate.toggle.user_context_factory';
if (null !== $config['context_factory']) {
$contextFactoryService = $config['context_factory'];
}

$container->setAlias('qandidate.toggle.context_factory', $contextFactoryService);
}
}
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014 Qandidate.com - http://qandidate.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.
18 changes: 18 additions & 0 deletions QandidateToggleBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Bundle\ToggleBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class QandidateToggleBundle extends Bundle
{
}
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Qandidate Toggle Symfony Bundle

This Bundle provides the integration with [our toggle library]. It provides the
services and configuration you need to implement feature toggles in your
application.

[![Build Status](https://travis-ci.org/qandidate-labs/qandidate-toggle-bundle.svg?branch=master)](https://travis-ci.org/qandidate-labs/qandidate-toggle-bundle)

[our toggle library]: https://github.com/qandidate-labs/qandidate-toggle

## About

Read the our blog post series about this repository at:
- http://labs.qandidate.com/blog/2014/08/18/a-new-feature-toggling-library-for-php/
- http://labs.qandidate.com/blog/2014/08/19/open-sourcing-our-feature-toggle-api-and-ui/

## Installation

Add the bundle to your composer.json

```bash
$ composer require qandidate-labs/qandidate-toggle-bundle ~0.1
```

Add the bundle to your Kernel

```php
$bundles = array(
// ..
new Qandidate\Bundle\ToggleBundle\QandidateToggleBundle(),
);
```
## Configuration

```yaml
qandidate_toggle:
persistence: in_memory|redis
context_factory: null|your.context_factory.service.id
redis_namespace: toggle_%kernel.environment% # default, only required when persistence = redis
redis_client: null|your.redis_client.service.id # only required when persistence = redis
```
## Example usage
Usage can vary on your application. This example uses the supplied
`UserContextFactory`, but you probably need to create your own factory.

```xml
<!-- services.xml -->
<service id="acme.controller" class="Acme\Controller">
<argument type="service" id="qandidate.toggle.manager" />
<argument type="service" id="qandidate.toggle.user_context_factory" />
</service>
```

```php
// Acme\Controller
public function __construct(
/* ArticleRepository, Templating, ..*/
ToggleManager $manager,
ContextFactory $contextFactory
) {
// ..
$this->manager = $manager;
$this->context = $contextFactory->createContext();
}
// ..
public function articleAction(Request $request)
{
$this->article = $this->repository->findBySlug($request->request->get('slug'));
return $this->templating->render('article.html.twig', array(
'article' => $article,
'enableComments' => $this->manager->active('comments', $this->context),
));
}
```

## Twig usage

If you use Twig you can also use the Twig function:

```jinja
{% if is_active('comments') %}
{# Awesome comments #}
{% endif %}
```

## License

MIT, see LICENSE.
15 changes: 15 additions & 0 deletions Resources/config/redis.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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">

<services>

<service id="qandidate.toggle.collection.predis" class="%qandidate.toggle.collection.predis.class%">
<argument>%qandidate.toggle.redis.namespace%</argument>
<argument type="service" id="qandidate.toggle.redis.client" />
</service>

</services>
</container>
33 changes: 33 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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="qandidate.toggle.manager.class">Qandidate\Toggle\ToggleManager</parameter>
<parameter key="qandidate.toggle.collection.in_memory.class">Qandidate\Toggle\ToggleCollection\InMemoryCollection</parameter>
<parameter key="qandidate.toggle.collection.predis.class">Qandidate\Toggle\ToggleCollection\PredisCollection</parameter>
<parameter key="qandidate.toggle.user_context_factory.class">Qandidate\Bundle\ToggleBundle\Context\UserContextFactory</parameter>
<parameter key="qandidate.toggle.twig_extension.class">Qandidate\Bundle\ToggleBundle\Twig\ToggleTwigExtension</parameter>
</parameters>

<services>

<service id="qandidate.toggle.collection.in_memory" class="%qandidate.toggle.collection.in_memory.class%" />

<service id="qandidate.toggle.manager" class="%qandidate.toggle.manager.class%">
<argument type="service" id="qandidate.toggle.collection" />
</service>

<service id="qandidate.toggle.user_context_factory" class="%qandidate.toggle.user_context_factory.class%">
<argument type="service" id="security.context" />
</service>

<service id="qandidate.toggle.twig_extension" class="%qandidate.toggle.twig_extension.class%">
<tag name="twig.extension" />
<argument type="service" id="qandidate.toggle.manager" />
<argument type="service" id="qandidate.toggle.context_factory" />
</service>
</services>
</container>
43 changes: 43 additions & 0 deletions Tests/Context/UserContextFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Bundle\ToggleBundle\Context;

use PHPUnit_Framework_TestCase;
use Qandidate\Bundle\ToggleBundle\Tests\SecurityContext;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;

class UserContextFactoryTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->securityContext = new SecurityContext();
$this->contextFactory = new UserContextFactory($this->securityContext);
}

/**
* @test
*/
public function it_should_set_the_username_when_available()
{
$this->securityContext->setToken(new AnonymousToken('key', 'foobar'));

$this->assertEquals('foobar', $this->contextFactory->createContext()->get('username'));
}

/**
* @test
*/
public function it_should_not_set_the_username_when_token_is_unavailable()
{
$this->assertFalse($this->contextFactory->createContext()->has('username'));
}
}
Loading

0 comments on commit 7477681

Please sign in to comment.