-
Notifications
You must be signed in to change notification settings - Fork 252
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 #58 from lookyman/nette-latte
Add Nette & Latte integrations
- Loading branch information
Showing
6 changed files
with
305 additions
and
2 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
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,37 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Bridge\Latte; | ||
|
||
use Cocur\Slugify\SlugifyInterface; | ||
|
||
/** | ||
* SlugifyHelper | ||
* | ||
* @package cocur/slugify | ||
* @subpackage bridge | ||
* @author Lukáš Unger <[email protected]> | ||
* @license http://www.opensource.org/licenses/MIT The MIT License | ||
*/ | ||
class SlugifyHelper | ||
{ | ||
/** @var SlugifyInterface */ | ||
private $slugify; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function __construct(SlugifyInterface $slugify) | ||
{ | ||
$this->slugify = $slugify; | ||
} | ||
|
||
/** | ||
* @param string | ||
* @param string | ||
* @return string | ||
*/ | ||
public function slugify($string, $separator = '-') | ||
{ | ||
return $this->slugify->slugify($string, $separator); | ||
} | ||
} |
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 Cocur\Slugify\Bridge\Nette; | ||
|
||
use Nette\DI\CompilerExtension; | ||
use Nette\DI\ServiceDefinition; | ||
|
||
/** | ||
* SlugifyExtension | ||
* | ||
* @package cocur/slugify | ||
* @subpackage bridge | ||
* @author Lukáš Unger <[email protected]> | ||
* @license http://www.opensource.org/licenses/MIT The MIT License | ||
*/ | ||
class SlugifyExtension extends CompilerExtension | ||
{ | ||
public function loadConfiguration() | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$builder->addDefinition($this->prefix('slugify')) | ||
->setClass('Cocur\Slugify\SlugifyInterface') | ||
->setFactory('Cocur\Slugify\Slugify'); | ||
|
||
$builder->addDefinition($this->prefix('helper')) | ||
->setClass('Cocur\Slugify\Bridge\Latte\SlugifyHelper') | ||
->setInject(false); | ||
} | ||
|
||
public function beforeCompile() | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$self = $this; | ||
$registerToLatte = function(ServiceDefinition $def) use ($self) { | ||
$def->addSetup('addFilter', array('slugify', array($self->prefix('@helper'), 'slugify'))); | ||
}; | ||
|
||
$latteFactory = $builder->getByType('Nette\Bridges\ApplicationLatte\ILatteFactory') ?: 'nette.latteFactory'; | ||
if ($builder->hasDefinition($latteFactory)) { | ||
$registerToLatte($builder->getDefinition($latteFactory)); | ||
} | ||
|
||
if ($builder->hasDefinition('nette.latte')) { | ||
$registerToLatte($builder->getDefinition('nette.latte')); | ||
} | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Bridge\Latte; | ||
|
||
use Cocur\Slugify\Bridge\Latte\SlugifyHelper; | ||
use \Mockery as m; | ||
|
||
/** | ||
* SlugifyHelperTest | ||
* | ||
* @category test | ||
* @package cocur/slugify | ||
* @subpackage bridge | ||
* @author Lukáš Unger <[email protected]> | ||
* @license http://www.opensource.org/licenses/MIT The MIT License | ||
* @group unit | ||
*/ | ||
class SlugifyHelperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->slugify = m::mock('Cocur\Slugify\SlugifyInterface'); | ||
$this->helper = new SlugifyHelper($this->slugify); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Cocur\Slugify\Bridge\Latte\SlugifyHelper::slugify() | ||
*/ | ||
public function slugify() | ||
{ | ||
$this->slugify->shouldReceive('slugify')->with('hällo wörld', '_')->once()->andReturn('haello_woerld'); | ||
|
||
$this->assertEquals('haello_woerld', $this->helper->slugify('hällo wörld', '_')); | ||
} | ||
} |
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,132 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Bridge\Nette; | ||
|
||
use \Mockery as m; | ||
|
||
/** | ||
* SlugifyExtensionTest | ||
* | ||
* @category test | ||
* @package cocur/slugify | ||
* @subpackage bridge | ||
* @author Lukáš Unger <[email protected]> | ||
* @license http://www.opensource.org/licenses/MIT The MIT License | ||
* @group unit | ||
*/ | ||
class SlugifyExtensionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->extension = new SlugifyExtension(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Cocur\Slugify\Bridge\Nette\SlugifyExtension::loadConfiguration() | ||
*/ | ||
public function loadConfiguration() | ||
{ | ||
$slugify = m::mock('Nette\DI\ServiceDefinition'); | ||
$slugify | ||
->shouldReceive('setClass') | ||
->with('Cocur\Slugify\SlugifyInterface') | ||
->once() | ||
->andReturn($slugify); | ||
$slugify | ||
->shouldReceive('setFactory') | ||
->with('Cocur\Slugify\Slugify') | ||
->once() | ||
->andReturn($slugify); | ||
|
||
$helper = m::mock('Nette\DI\ServiceDefinition'); | ||
$helper | ||
->shouldReceive('setClass') | ||
->with('Cocur\Slugify\Bridge\Latte\SlugifyHelper') | ||
->once() | ||
->andReturn($helper); | ||
$helper | ||
->shouldReceive('setInject') | ||
->with(false) | ||
->once() | ||
->andReturn($helper); | ||
|
||
$builder = m::mock('Nette\DI\ContainerBuilder'); | ||
$builder | ||
->shouldReceive('addDefinition') | ||
->with('slugify.slugify') | ||
->once() | ||
->andReturn($slugify); | ||
$builder | ||
->shouldReceive('addDefinition') | ||
->with('slugify.helper') | ||
->once() | ||
->andReturn($helper); | ||
|
||
$compiler = m::mock('Nette\DI\Compiler'); | ||
$compiler | ||
->shouldReceive('getContainerBuilder') | ||
->once() | ||
->andReturn($builder); | ||
|
||
$this->extension->setCompiler($compiler, 'slugify'); | ||
$this->extension->loadConfiguration(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Cocur\Slugify\Bridge\Nette\SlugifyExtension::beforeCompile() | ||
*/ | ||
public function beforeCompile() | ||
{ | ||
$latteFactory = m::mock('Nette\DI\ServiceDefinition'); | ||
$latteFactory | ||
->shouldReceive('addSetup') | ||
->with('addFilter', array('slugify', array('@slugify.helper', 'slugify'))) | ||
->once() | ||
->andReturn($latteFactory); | ||
|
||
$latte = m::mock('Nette\DI\ServiceDefinition'); | ||
$latte | ||
->shouldReceive('addSetup') | ||
->with('addFilter', array('slugify', array('@slugify.helper', 'slugify'))) | ||
->once() | ||
->andReturn($latte); | ||
|
||
$builder = m::mock('Nette\DI\ContainerBuilder'); | ||
$builder | ||
->shouldReceive('getByType') | ||
->with('Nette\Bridges\ApplicationLatte\ILatteFactory') | ||
->once() | ||
->andReturn('latte.latteFactory'); | ||
$builder | ||
->shouldReceive('hasDefinition') | ||
->with('latte.latteFactory') | ||
->once() | ||
->andReturn(true); | ||
$builder | ||
->shouldReceive('getDefinition') | ||
->with('latte.latteFactory') | ||
->once() | ||
->andReturn($latteFactory); | ||
$builder | ||
->shouldReceive('hasDefinition') | ||
->with('nette.latte') | ||
->once() | ||
->andReturn(true); | ||
$builder | ||
->shouldReceive('getDefinition') | ||
->with('nette.latte') | ||
->once() | ||
->andReturn($latte); | ||
|
||
$compiler = m::mock('Nette\DI\Compiler'); | ||
$compiler | ||
->shouldReceive('getContainerBuilder') | ||
->once() | ||
->andReturn($builder); | ||
|
||
$this->extension->setCompiler($compiler, 'slugify'); | ||
$this->extension->beforeCompile(); | ||
} | ||
} |