Skip to content

Commit

Permalink
Merge pull request #25 from xavismeh/master
Browse files Browse the repository at this point in the history
[WIP] Added get_option helper in twig when available
  • Loading branch information
eko committed Jun 30, 2014
2 parents 2d93923 + 828a917 commit 125ea77
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 3 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -37,6 +37,7 @@ public function getConfigTreeBuilder()
->scalarNode('table_prefix')->end()
->scalarNode('wordpress_directory')->end()
->scalarNode('entity_manager')->end()
->booleanNode('load_twig_extension')->defaultFalse()->end()
->end()
;

Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/EkinoWordpressExtension.php
Expand Up @@ -51,6 +51,10 @@ public function load(array $configs, ContainerBuilder $container)
if (isset($config['entity_manager'])) {
$this->loadEntityManager($container, $config['entity_manager']);
}

if ($config['load_twig_extension']) {
$loader->load('twig.xml');
}
}

/**
Expand Down
2 changes: 0 additions & 2 deletions Manager/OptionManager.php
Expand Up @@ -10,8 +10,6 @@

namespace Ekino\WordpressBundle\Manager;

use Ekino\WordpressBundle\Manager\BaseManager;

/**
* Class OptionManager
*
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -67,9 +67,10 @@ Optionnally, you can specify the following options in your `app/config.yml`:
ekino_wordpress:
table_prefix: wp_ # If you have a specific Wordpress table prefix
wordpress_directory: /my/wordpress/directory # If you have a specific Wordpress directory structure
load_twig_extension: true # If you want to enable native WordPress functions (ie : get_option() => wp_get_option())
```

Also optionnally, if you want to use `UserHook` to authenticate on Symfony, you should add this configuration to your `app/security.yml`:
Also optionally, if you want to use `UserHook` to authenticate on Symfony, you should add this configuration to your `app/security.yml`:

```yml
security:
Expand Down
19 changes: 19 additions & 0 deletions Resources/config/twig.xml
@@ -0,0 +1,19 @@
<?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="ekino.wordpress.twig.extension.option.class">Ekino\WordpressBundle\Twig\Extension\OptionExtension</parameter>
</parameters>

<services>
<service id="ekino.wordpress.twig.extension.option" class="%ekino.wordpress.twig.extension.option.class%">
<tag name="twig.extension" />

<argument type="service" id="ekino.wordpress.manager.option" />
</service>

</services>
</container>
72 changes: 72 additions & 0 deletions Tests/Twig/Extension/OptionExtensionTest.php
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Ekino Wordpress package.
*
* (c) 2013 Ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ekino\WordpressBundle\Tests\Twig\Extension;

use Ekino\WordpressBundle\Twig\Extension\OptionExtension;

/**
* Class OptionExtensionTest
*
* @author Xavier Coureau <xav.is@2cool4school.fr>
*/
class OptionExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $optionManager;

/**
* @var OptionExtension
*/
protected $extension;

protected function setUp()
{
if (!class_exists('\Twig_Extension')) {
$this->markTestSkipped('Twig is not enabled');
}

$this->optionManager = $this->getMockBuilder('Ekino\WordpressBundle\Manager\OptionManager')->disableOriginalConstructor()->getMock();
$this->extension = new OptionExtension($this->optionManager);
}

/**
* Check the correct result for an existing option
*/
public function testGetOption()
{
$optionMock = $this->getMock('Ekino\WordpressBundle\Entity\Option');

$this->optionManager->expects($this->once())
->method('findOneByOptionName')
->with($this->equalTo('test'))
->will($this->returnValue($optionMock));

$result = $this->extension->getOption('test');
$this->assertEquals($optionMock, $result);
}

/**
* Check the usage of default return value for a non existing option
*/
public function testGetOptionUndefined()
{
$this->optionManager->expects($this->once())
->method('findOneByOptionName')
->with($this->equalTo('test'))
->will($this->returnValue(null));

$result = $this->extension->getOption('test', 'poney');
$this->assertEquals('poney', $result);
}
}
52 changes: 52 additions & 0 deletions Twig/Extension/OptionExtension.php
@@ -0,0 +1,52 @@
<?php

namespace Ekino\WordpressBundle\Twig\Extension;

use Ekino\WordpressBundle\Manager\OptionManager;

class OptionExtension extends \Twig_Extension
{
/**
* @var OptionManager
*/
protected $optionManager;

/**
* @return string
*/
public function getName()
{
return 'ekino_wordpress_option';
}

/**
* @param OptionManager $optionManager
*/
public function __construct(OptionManager $optionManager)
{
$this->optionManager = $optionManager;
}

/**
* @return array
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('wp_get_option', array($this, 'getOption')),
);
}

/**
* @param string $optionName
* @param mixed $defaultValue
*
* @return mixed
*/
public function getOption($optionName, $defaultValue = null)
{
$option = $this->optionManager->findOneByOptionName($optionName);

return $option ?: $defaultValue;
}
}

0 comments on commit 125ea77

Please sign in to comment.