-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from alquerci/add/emoticon/PhpFileLoader
Added support to load emoticons configuration from a php file
- Loading branch information
Showing
6 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
return $loader->import('simple.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |