Skip to content

Commit

Permalink
Merge pull request #97 from alquerci/add/emoticon/PhpFileLoader
Browse files Browse the repository at this point in the history
Added support to load emoticons configuration from a php file
  • Loading branch information
helios-ag committed Mar 1, 2015
2 parents 3010cfc + ad221ff commit f80f398
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Decoda/Hook/EmoticonHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Loader\LoaderInterface;

use Decoda\Decoda;
use \Decoda\Hook\EmoticonHook as BaseEmoticonHook;

use FM\BbcodeBundle\Emoticon\Emoticon;
use FM\BbcodeBundle\Emoticon\EmoticonCollection;
use FM\BbcodeBundle\Emoticon\Matcher\MatcherInterface;
use FM\BbcodeBundle\Emoticon\Loader\FileLoader;


/**
Expand All @@ -27,7 +27,7 @@
class EmoticonHook extends BaseEmoticonHook implements CacheWarmerInterface
{
/**
* @var FileLoader
* @var LoaderInterface
*/
protected $loader;

Expand Down Expand Up @@ -55,11 +55,11 @@ class EmoticonHook extends BaseEmoticonHook implements CacheWarmerInterface
/**
* Constructor.
*
* @param FileLoader $loader A LoaderInterface instance
* @param LoaderInterface $loader A LoaderInterface instance
* @param mixed $resource The main resource to load
* @param array $options An array of options
*/
public function __construct(FileLoader $loader, ContainerInterface $container, array $options = array())
public function __construct(LoaderInterface $loader, ContainerInterface $container, array $options = array())
{
$this->loader = $loader;
$this->container = $container;
Expand Down
46 changes: 46 additions & 0 deletions Emoticon/Loader/PhpFileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace FM\BbcodeBundle\Emoticon\Loader;

use Symfony\Component\Config\Resource\FileResource;

use FM\BbcodeBundle\Emoticon\EmoticonCollection;


/**
* PhpFileLoader loads PHP files emoticons.
*
* @author Alexandre Quercia <[email protected]>
*/
class PhpFileLoader extends FileLoader
{
/**
* Loads the resource and return the result.
*
* @param mixed $resource
* @param string $type
*
* @return EmoticonCollection
*/
public function load($resource, $type = null)
{
$path = $this->locator->locate($resource);
$this->setCurrentDir(dirname($path));

// the loader variable is exposed to the included file below
$loader = $this;

$collection = include $path;
$collection->addResource(new FileResource($path));

return $collection;
}

/**
* {@inheritdoc}
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
}
}
18 changes: 17 additions & 1 deletion Resources/config/hooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<parameter key="fm_bbcode.decoda.hook.emoticon.class">FM\BbcodeBundle\Decoda\Hook\EmoticonHook</parameter>
<parameter key="fm_bbcode.decoda.hook.code.class">Decoda\Hook\CodeHook</parameter>

<parameter key="fm_bbcode.emoticon.delegating_loader.class">Symfony\Component\Config\Loader\DelegatingLoader</parameter>
<parameter key="fm_bbcode.emoticon.loader_resolver.class">Symfony\Component\Config\Loader\LoaderResolver</parameter>
<parameter key="fm_bbcode.emoticon.loader.yaml.class">FM\BbcodeBundle\Emoticon\Loader\YamlFileLoader</parameter>
<parameter key="fm_bbcode.emoticon.loader.php.class">FM\BbcodeBundle\Emoticon\Loader\PhpFileLoader</parameter>
<parameter key="fm_bbcode.emoticon.options.matcher_class">FM\BbcodeBundle\Emoticon\Matcher\Matcher</parameter>
<parameter key="fm_bbcode.emoticon.options.matcher_base_class">FM\BbcodeBundle\Emoticon\Matcher\Matcher</parameter>
<parameter key="fm_bbcode.emoticon.options.matcher_dumper_class">FM\BbcodeBundle\Emoticon\Matcher\Dumper\PhpMatcherDumper</parameter>
Expand All @@ -27,7 +30,7 @@
<service id="fm_bbcode.decoda.hook.emoticon" class="%fm_bbcode.decoda.hook.emoticon.class%">
<tag name="fm_bbcode.decoda.hook" id="emoticon" />
<tag name="kernel.cache_warmer" />
<argument type="service" id="fm_bbcode.emoticon.loader.yaml" />
<argument type="service" id="fm_bbcode.emoticon.loader" />
<argument type="service" id="service_container" />
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
Expand All @@ -42,8 +45,21 @@
<tag name="fm_bbcode.decoda.hook" id="code" />
</service>

<service id="fm_bbcode.emoticon.loader" alias="fm_bbcode.emoticon.delegating_loader" />
<service id="fm_bbcode.emoticon.delegating_loader" class="%fm_bbcode.emoticon.delegating_loader.class%">
<argument type="service" id="fm_bbcode.emoticon.loader_resolver" />
</service>
<service id="fm_bbcode.emoticon.loader_resolver" class="%fm_bbcode.emoticon.loader_resolver.class%">
<argument type="collection">
<argument type="service" id="fm_bbcode.emoticon.loader.yaml" />
<argument type="service" id="fm_bbcode.emoticon.loader.php" />
</argument>
</service>
<service id="fm_bbcode.emoticon.loader.yaml" class="%fm_bbcode.emoticon.loader.yaml.class%">
<argument type="service" id="file_locator"/>
</service>
<service id="fm_bbcode.emoticon.loader.php" class="%fm_bbcode.emoticon.loader.php.class%">
<argument type="service" id="file_locator"/>
</service>
</services>
</container>
49 changes: 49 additions & 0 deletions Tests/Emoticon/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace FM\BbcodeBundle\Tests\Emoticon\Loader;

use Symfony\Component\Config\FileLocator;

use FM\BbcodeBundle\Emoticon\Loader\PhpFileLoader;

/**
* @author Alexandre Quercia <[email protected]>
*/
class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers FM\BbcodeBundle\Emoticon\Loader\PhpFileLoader::supports
*/
public function testSupports()
{
$loader = new PhpFileLoader(new FileLocator());

$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
}

/**
* @covers FM\BbcodeBundle\Emoticon\Loader\PhpFileLoader::load
*/
public function testLoad()
{
$loader = new PhpFileLoader(new FileLocator());

$collection = $loader->load(__DIR__.'/../../Fixtures/Emoticon/php/simple.php');

$this->assertInstanceOf('FM\BbcodeBundle\Emoticon\EmoticonCollection', $collection);
$this->assertTrue($collection->has('foo'), '->load() loads a PHP file resource');
$this->assertCount(1, $collection->getResources());
}

/**
* @covers FM\BbcodeBundle\Emoticon\Loader\PhpFileLoader::load
*/
public function testLoadSupportImport()
{
$loader = new PhpFileLoader(new FileLocator());

$collection = $loader->load(__DIR__.'/../../Fixtures/Emoticon/php/import.php');
$this->assertCount(2, $collection->getResources());
}
}
3 changes: 3 additions & 0 deletions Tests/Fixtures/Emoticon/php/import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return $loader->import('simple.php');
13 changes: 13 additions & 0 deletions Tests/Fixtures/Emoticon/php/simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use FM\BbcodeBundle\Emoticon\EmoticonCollection;
use FM\BbcodeBundle\Emoticon\Emoticon;

$collection = new EmoticonCollection();

$emoticon = new Emoticon();
$emoticon->addSmilies(array(':foo:'));

$collection->add('foo', $emoticon);

return $collection;

0 comments on commit f80f398

Please sign in to comment.