Created
November 26, 2013 20:22
-
-
Save ShawnMcCool/7665559 to your computer and use it in GitHub Desktop.
Simple Blade Tag Abstraction.
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
class BladeParser | |
{ | |
protected $tags = []; | |
public function parse($view) | |
{ | |
foreach ($this->tags as $tag) { | |
if ($tag->getMatchCount($view) > 0) $view = $tag->transform($view); | |
} | |
return $view; | |
} | |
public function addTag(BladeTag $tag) | |
{ | |
$this->tags[] = $tag; | |
} | |
} |
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
use Codeception\Util\Stub; | |
use Mockery; | |
class BladeParserTest extends \Codeception\TestCase\Test | |
{ | |
protected $codeGuy; | |
protected function _before() {} | |
protected function _after() {} | |
// tests | |
public function testCanCreate() | |
{ | |
$this->assertInstanceOf('Sterling\BladeParsing\BladeParser', $this->getParser()); | |
} | |
public function testCanParseWithoutTags() | |
{ | |
$parser = $this->getParser(); | |
$text = "But it's the truth even if it didn't happen."; | |
$this->assertEquals($text, $parser->parse($text)); | |
} | |
public function testCanParseWithTags() | |
{ | |
$parser = $this->getParser(); | |
// one tag | |
$tag = Mockery::mock('Sterling\BladeParsing\BladeTag'); | |
$tag->shouldReceive('getMatchCount')->andReturn(1); | |
$tag->shouldReceive('transform')->andReturn('robots'); | |
$parser->addTag($tag); | |
$text = "But it's the truth even if it didn't happen."; | |
$this->assertEquals('robots', $parser->parse($text)); | |
// two tags | |
$tag = Mockery::mock('Sterling\BladeParsing\BladeTag'); | |
$tag->shouldReceive('getMatchCount')->andReturn(2); | |
$tag->shouldReceive('transform')->andReturn('cats'); | |
$parser->addTag($tag); | |
$text = "But it's the truth even if it didn't happen."; | |
$this->assertEquals('cats', $parser->parse($text)); | |
} | |
// ---------- private ------------ // | |
private function getParser() | |
{ | |
return new BladeParser; | |
} | |
} |
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
interface BladeTag | |
{ | |
public function getPattern(); | |
public function getMatchCount($view); | |
public function transform($view); | |
} |
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
class LayoutTag implements BladeTag | |
{ | |
protected $pattern = '/@layout\([\'"](.*?)[\'"]\)/'; | |
public function getPattern() | |
{ | |
return $this->pattern; | |
} | |
public function getMatchCount($view) | |
{ | |
return count($this->getMatches($view)); | |
} | |
public function transform($view) | |
{ | |
return $this->processView($view); | |
} | |
private function processView($view) | |
{ | |
$matches = $this->getMatches($view); | |
if (empty($matches)) return $view; | |
foreach($matches as $match) { | |
list($tag, $viewName) = $match; | |
$view = $this->replaceTag($view, $tag, $viewName); | |
} | |
return $view; | |
} | |
private function getMatches($view) | |
{ | |
preg_match_all($this->pattern, $view, $matches, PREG_SET_ORDER); | |
return $matches; | |
} | |
private function replaceTag($view, $tag, $viewName) | |
{ | |
return str_replace($tag, '@extends($currentSite->template->getView("layouts.' . $viewName . '"))', $view); | |
} | |
} |
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
use Codeception\Util\Stub; | |
use Mockery; | |
class LayoutTagTest extends \Codeception\TestCase\Test | |
{ | |
protected $codeGuy; | |
protected function _before() {} | |
protected function _after() {} | |
// tests | |
public function testCanCreate() | |
{ | |
$this->assertInstanceOf('Sterling\BladeParsing\LayoutTag', $this->getTag()); | |
} | |
public function testCanGetPattern() | |
{ | |
$tag = $this->getTag(); | |
$this->assertNotEmpty($tag->getPattern()); | |
} | |
public function testCanTransformWithoutMatch() | |
{ | |
$tag = $this->getTag(); | |
$view = "All I know is this: nobody's very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down."; | |
$this->assertEquals(0, $tag->getmatchCount($view)); | |
$this->assertEquals($view, $tag->transform($view)); | |
} | |
public function testCanTransformSingleMatch() | |
{ | |
$tag = $this->getTag(); | |
$view = 'All I know is this: @layout("default")nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down.'; | |
$expected = 'All I know is this: @extends($currentSite->template->getView("layouts.default"))nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down.'; | |
$this->assertEquals(1, $tag->getmatchCount($view)); | |
$this->assertEquals($expected, $tag->transform($view)); | |
} | |
public function testCanTransformMultipleMatches() | |
{ | |
$tag = $this->getTag(); | |
$view = 'All I know is this: @layout("default")nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down@layout("cats").'; | |
$expected = 'All I know is this: @extends($currentSite->template->getView("layouts.default"))nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down@extends($currentSite->template->getView("layouts.cats")).'; | |
$this->assertEquals(2, $tag->getmatchCount($view)); | |
$this->assertEquals($expected, $tag->transform($view)); | |
} | |
// ---------- private ------------ // | |
private function getTag() | |
{ | |
return new LayoutTag; | |
} | |
} |
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
class PartialTag implements BladeTag | |
{ | |
protected $pattern = '/@partial\([\'"](.*?)[\'"]\)/'; | |
public function getPattern() | |
{ | |
return $this->pattern; | |
} | |
public function getMatchCount($view) | |
{ | |
return count($this->getMatches($view)); | |
} | |
public function transform($view) | |
{ | |
return $this->processView($view); | |
} | |
private function processView($view) | |
{ | |
$matches = $this->getMatches($view); | |
if (empty($matches)) return $view; | |
foreach($matches as $match) { | |
list($tag, $viewName) = $match; | |
$view = $this->replaceTag($view, $tag, $viewName); | |
} | |
return $view; | |
} | |
private function getMatches($view) | |
{ | |
preg_match_all($this->pattern, $view, $matches, PREG_SET_ORDER); | |
return $matches; | |
} | |
private function replaceTag($view, $tag, $viewName) | |
{ | |
return str_replace($tag, '@include($currentSite->template->getView("' . $viewName . '"))', $view); | |
} | |
} |
This file contains hidden or 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
<?php namespace Sterling\BladeParsing; | |
use Codeception\Util\Stub; | |
use Mockery; | |
use Sterling\Templates\Template; | |
class PartialTagTest extends \Codeception\TestCase\Test | |
{ | |
protected $codeGuy; | |
protected $template; | |
protected function _before() {} | |
protected function _after() {} | |
// tests | |
public function testCanCreate() | |
{ | |
$this->assertInstanceOf('Sterling\BladeParsing\PartialTag', $this->getTag()); | |
} | |
public function testCanGetPattern() | |
{ | |
$tag = $this->getTag(); | |
$this->assertNotEmpty($tag->getPattern()); | |
} | |
public function testCanTransformWithoutMatch() | |
{ | |
$tag = $this->getTag(); | |
$view = "All I know is this: nobody's very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down."; | |
$this->assertEquals(0, $tag->getmatchCount($view)); | |
$this->assertEquals($view, $tag->transform($view)); | |
} | |
public function testCanTransformSingleMatch() | |
{ | |
$tag = $this->getTag(); | |
$view = "All I know is this: @partial('layouts.default')nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down."; | |
$expected = 'All I know is this: @include($currentSite->template->getView("layouts.default"))nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down.'; | |
$this->assertEquals(1, $tag->getmatchCount($view)); | |
$this->assertEquals($expected, $tag->transform($view)); | |
} | |
public function testCanTransformMultipleMatches() | |
{ | |
$tag = $this->getTag(); | |
$view = 'All I know is this: @partial("layouts.default")nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down@partial("cats").'; | |
$expected = 'All I know is this: @include($currentSite->template->getView("layouts.default"))nobodys very big in the first place, and it looks to me like everybody spends their whole life tearing everybody else down@include($currentSite->template->getView("cats")).'; | |
$this->assertEquals(2, $tag->getmatchCount($view)); | |
$this->assertEquals($expected, $tag->transform($view)); | |
} | |
// ---------- private ------------ // | |
private function getTag() | |
{ | |
return new PartialTag; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment