From 3fd8edc63b32c30c2c60670b707347b027c478e9 Mon Sep 17 00:00:00 2001 From: An Phan Date: Wed, 11 Mar 2015 21:50:57 +0800 Subject: [PATCH 1/3] Refactored View package --- .../View/Compilers/BladeCompiler.php | 34 +++++------ .../View/Engines/CompilerEngine.php | 2 +- .../View/Engines/EngineResolver.php | 4 +- src/Illuminate/View/Factory.php | 60 +++++++++++-------- src/Illuminate/View/FileViewFinder.php | 15 +++-- .../Middleware/ShareErrorsFromSession.php | 4 +- src/Illuminate/View/ViewServiceProvider.php | 2 +- 7 files changed, 65 insertions(+), 56 deletions(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index b7b38ffe315c..a3e92017b5d6 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -9,7 +9,7 @@ class BladeCompiler extends Compiler implements CompilerInterface { * * @var array */ - protected $extensions = array(); + protected $extensions = []; /** * The file currently being compiled. @@ -23,33 +23,33 @@ class BladeCompiler extends Compiler implements CompilerInterface { * * @var array */ - protected $compilers = array( + protected $compilers = [ 'Extensions', 'Statements', 'Comments', 'Echos' - ); + ]; /** * Array of opening and closing tags for raw echos. * * @var array */ - protected $rawTags = array('{!!', '!!}'); + protected $rawTags = ['{!!', '!!}']; /** * Array of opening and closing tags for regular echos. * * @var array */ - protected $contentTags = array('{{', '}}'); + protected $contentTags = ['{{', '}}']; /** * Array of opening and closing tags for escaped echos. * * @var array */ - protected $escapedTags = array('{{{', '}}}'); + protected $escapedTags = ['{{{', '}}}']; /** * The "regular" / legacy echo string format. @@ -63,7 +63,7 @@ class BladeCompiler extends Compiler implements CompilerInterface { * * @var array */ - protected $footer = array(); + protected $footer = []; /** * Counter to keep track of nested forelse statements. @@ -80,7 +80,7 @@ class BladeCompiler extends Compiler implements CompilerInterface { */ public function compile($path = null) { - $this->footer = array(); + $this->footer = []; if ($path) { @@ -319,7 +319,7 @@ protected function compileEscapedEchos($value) { $whitespace = empty($matches[2]) ? '' : $matches[2].$matches[2]; - return 'compileEchoDefaults($matches[1]).'); ?>'.$whitespace; + return 'compileEchoDefaults($matches[1]).'); ?>'.$whitespace; }; return preg_replace_callback($pattern, $callback, $value); @@ -344,7 +344,7 @@ public function compileEchoDefaults($value) */ protected function compileEach($expression) { - return "renderEach{$expression}; ?>"; + return "renderEach{$expression}; ?>"; } /** @@ -368,7 +368,7 @@ protected function compileInject($expression) */ protected function compileYield($expression) { - return "yieldContent{$expression}; ?>"; + return "yieldContent{$expression}; ?>"; } /** @@ -379,7 +379,7 @@ protected function compileYield($expression) */ protected function compileShow($expression) { - return "yieldSection(); ?>"; + return "yieldSection(); ?>"; } /** @@ -467,7 +467,7 @@ protected function compileEndunless($expression) */ protected function compileLang($expression) { - return ""; + return ""; } /** @@ -478,7 +478,7 @@ protected function compileLang($expression) */ protected function compileChoice($expression) { - return ""; + return ""; } /** @@ -641,7 +641,7 @@ protected function compileExtends($expression) $expression = substr($expression, 1, -1); } - $data = "make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>"; + $data = "make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>"; $this->footer[] = $data; @@ -661,7 +661,7 @@ protected function compileInclude($expression) $expression = substr($expression, 1, -1); } - return "make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>"; + return "make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>"; } /** @@ -672,7 +672,7 @@ protected function compileInclude($expression) */ protected function compileStack($expression) { - return "yieldContent{$expression}; ?>"; + return "yieldContent{$expression}; ?>"; } /** diff --git a/src/Illuminate/View/Engines/CompilerEngine.php b/src/Illuminate/View/Engines/CompilerEngine.php index 1fe35241240a..9c479b7be1a6 100755 --- a/src/Illuminate/View/Engines/CompilerEngine.php +++ b/src/Illuminate/View/Engines/CompilerEngine.php @@ -17,7 +17,7 @@ class CompilerEngine extends PhpEngine { * * @var array */ - protected $lastCompiled = array(); + protected $lastCompiled = []; /** * Create a new Blade view engine instance. diff --git a/src/Illuminate/View/Engines/EngineResolver.php b/src/Illuminate/View/Engines/EngineResolver.php index b8ddd5068651..45436c4a519d 100755 --- a/src/Illuminate/View/Engines/EngineResolver.php +++ b/src/Illuminate/View/Engines/EngineResolver.php @@ -10,14 +10,14 @@ class EngineResolver { * * @var array */ - protected $resolvers = array(); + protected $resolvers = []; /** * The resolved engine instances. * * @var array */ - protected $resolved = array(); + protected $resolved = []; /** * Register a new engine resolver. diff --git a/src/Illuminate/View/Factory.php b/src/Illuminate/View/Factory.php index 1587bf4f2559..8717b2af6ad2 100755 --- a/src/Illuminate/View/Factory.php +++ b/src/Illuminate/View/Factory.php @@ -43,49 +43,49 @@ class Factory implements FactoryContract { * * @var array */ - protected $shared = array(); + protected $shared = []; /** * Array of registered view name aliases. * * @var array */ - protected $aliases = array(); + protected $aliases = []; /** * All of the registered view names. * * @var array */ - protected $names = array(); + protected $names = []; /** * The extension to engine bindings. * * @var array */ - protected $extensions = array('blade.php' => 'blade', 'php' => 'php'); + protected $extensions = ['blade.php' => 'blade', 'php' => 'php']; /** * The view composer events. * * @var array */ - protected $composers = array(); + protected $composers = []; /** * All of the finished, captured sections. * * @var array */ - protected $sections = array(); + protected $sections = []; /** * The stack of in-progress sections. * * @var array */ - protected $sectionStack = array(); + protected $sectionStack = []; /** * The number of active rendering operations. @@ -138,7 +138,10 @@ public function file($path, $data = array(), $mergeData = array()) */ public function make($view, $data = array(), $mergeData = array()) { - if (isset($this->aliases[$view])) $view = $this->aliases[$view]; + if (isset($this->aliases[$view])) + { + $view = $this->aliases[$view]; + } $view = $this->normalizeName($view); @@ -162,7 +165,7 @@ protected function normalizeName($name) { $delimiter = ViewFinderInterface::HINT_PATH_DELIMITER; - if (strpos($name, $delimiter) === false) + if ( ! str_contains($name, $delimiter)) { return str_replace('/', '.', $name); } @@ -255,11 +258,11 @@ public function renderEach($view, $data, $iterator, $empty = 'raw|') // If is actually data in the array, we will loop through the data and append // an instance of the partial view to the final result HTML passing in the // iterated value of this data array, allowing the views to access them. - if (count($data) > 0) + if (count($data)) { foreach ($data as $key => $value) { - $data = array('key' => $key, $iterator => $value); + $data = ['key' => $key, $iterator => $value]; $result .= $this->make($view, $data)->render(); } @@ -298,9 +301,7 @@ public function getEngineFromPath($path) throw new InvalidArgumentException("Unrecognized extension in file: $path"); } - $engine = $this->extensions[$extension]; - - return $this->engines->resolve($engine); + return $this->engines->resolve($this->extensions[$extension]); } /** @@ -328,7 +329,10 @@ protected function getExtension($path) */ public function share($key, $value = null) { - if ( ! is_array($key)) return $this->shared[$key] = $value; + if ( ! is_array($key)) + { + return $this->shared[$key] = $value; + } foreach ($key as $innerKey => $innerValue) { @@ -345,7 +349,7 @@ public function share($key, $value = null) */ public function creator($views, $callback) { - $creators = array(); + $creators = []; foreach ((array) $views as $view) { @@ -363,7 +367,7 @@ public function creator($views, $callback) */ public function composers(array $composers) { - $registered = array(); + $registered = []; foreach ($composers as $callback => $views) { @@ -383,7 +387,7 @@ public function composers(array $composers) */ public function composer($views, $callback, $priority = null) { - $composers = array(); + $composers = []; foreach ((array) $views as $view) { @@ -412,7 +416,8 @@ protected function addViewEvent($view, $callback, $prefix = 'composing: ', $prio return $callback; } - elseif (is_string($callback)) + + if (is_string($callback)) { return $this->addClassEvent($view, $callback, $prefix, $priority); } @@ -477,7 +482,7 @@ protected function buildClassEventCallback($class, $prefix) // given arguments that are passed to the Closure as the composer's data. return function() use ($class, $method) { - $callable = array($this->container->make($class), $method); + $callable = [$this->container->make($class), $method]; return call_user_func_array($callable, func_get_args()); }; @@ -499,7 +504,7 @@ protected function parseClassEvent($class, $prefix) $method = str_contains($prefix, 'composing') ? 'compose' : 'create'; - return array($class, $method); + return [$class, $method]; } /** @@ -521,7 +526,7 @@ public function callComposer(View $view) */ public function callCreator(View $view) { - $this->events->fire('creating: '.$view->getName(), array($view)); + $this->events->fire('creating: '.$view->getName(), $view); } /** @@ -658,9 +663,9 @@ public function yieldContent($section, $default = '') */ public function flushSections() { - $this->sections = array(); + $this->sections = []; - $this->sectionStack = array(); + $this->sectionStack = []; } /** @@ -670,7 +675,10 @@ public function flushSections() */ public function flushSectionsIfDoneRendering() { - if ($this->doneRendering()) $this->flushSections(); + if ($this->doneRendering()) + { + $this->flushSections(); + } } /** @@ -757,7 +765,7 @@ public function addExtension($extension, $engine, $resolver = null) unset($this->extensions[$extension]); - $this->extensions = array_merge(array($extension => $engine), $this->extensions); + $this->extensions = array_merge([$extension => $engine], $this->extensions); } /** diff --git a/src/Illuminate/View/FileViewFinder.php b/src/Illuminate/View/FileViewFinder.php index 94efedb72a3a..ecb83ffc279c 100755 --- a/src/Illuminate/View/FileViewFinder.php +++ b/src/Illuminate/View/FileViewFinder.php @@ -24,21 +24,21 @@ class FileViewFinder implements ViewFinderInterface { * * @var array */ - protected $views = array(); + protected $views = []; /** * The namespace to file path hints. * * @var array */ - protected $hints = array(); + protected $hints = []; /** * Register a view extension with the finder. * * @var array */ - protected $extensions = array('blade.php', 'php'); + protected $extensions = ['blade.php', 'php']; /** * Create a new file view loader instance. @@ -67,7 +67,10 @@ public function __construct(Filesystem $files, array $paths, array $extensions = */ public function find($name) { - if (isset($this->views[$name])) return $this->views[$name]; + if (isset($this->views[$name])) + { + return $this->views[$name]; + } if ($this->hasHintInformation($name = trim($name))) { @@ -130,7 +133,7 @@ protected function findInPaths($name, $paths) { foreach ($this->getPossibleViewFiles($name) as $file) { - if ($this->files->exists($viewPath = $path.'/'.$file)) + if ($this->files->exists($viewPath = "$path/$file")) { return $viewPath; } @@ -150,7 +153,7 @@ protected function getPossibleViewFiles($name) { return array_map(function($extension) use ($name) { - return str_replace('.', '/', $name).'.'.$extension; + return str_replace('.', '/', $name).".$extension"; }, $this->extensions); } diff --git a/src/Illuminate/View/Middleware/ShareErrorsFromSession.php b/src/Illuminate/View/Middleware/ShareErrorsFromSession.php index 956360c2962d..34fbd1ccb022 100644 --- a/src/Illuminate/View/Middleware/ShareErrorsFromSession.php +++ b/src/Illuminate/View/Middleware/ShareErrorsFromSession.php @@ -39,9 +39,7 @@ public function handle($request, Closure $next) // without having to bind. An empty bag is set when there aren't errors. if ($request->session()->has('errors')) { - $this->view->share( - 'errors', $request->session()->get('errors') - ); + $this->view->share('errors', $request->session()->get('errors')); } // Putting the errors in the view for every view allows the developer to just diff --git a/src/Illuminate/View/ViewServiceProvider.php b/src/Illuminate/View/ViewServiceProvider.php index 83e3b76c6c8e..60d038605e1a 100755 --- a/src/Illuminate/View/ViewServiceProvider.php +++ b/src/Illuminate/View/ViewServiceProvider.php @@ -36,7 +36,7 @@ public function registerEngineResolver() // Next we will register the various engines with the resolver so that the // environment can resolve the engines it needs for various views based // on the extension of view files. We call a method for each engines. - foreach (array('php', 'blade') as $engine) + foreach (['php', 'blade'] as $engine) { $this->{'register'.ucfirst($engine).'Engine'}($resolver); } From e599591e7de3e4bfd80ce7026a762cdf24913b95 Mon Sep 17 00:00:00 2001 From: An Phan Date: Wed, 11 Mar 2015 22:07:55 +0800 Subject: [PATCH 2/3] Completed tests --- .../View/Compilers/BladeCompiler.php | 4 +- tests/View/ViewBladeCompilerTest.php | 140 +++++++++--------- 2 files changed, 72 insertions(+), 72 deletions(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index a3e92017b5d6..ad67a87c2bf0 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -277,7 +277,7 @@ protected function compileRawEchos($value) { $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3]; - return $matches[1] ? substr($matches[0], 1) : 'compileEchoDefaults($matches[2]).'; ?>'.$whitespace; + return $matches[1] ? substr($matches[0], 1) : 'compileEchoDefaults($matches[2]).'; ?>'.$whitespace; }; return preg_replace_callback($pattern, $callback, $value); @@ -299,7 +299,7 @@ protected function compileRegularEchos($value) $wrapped = sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2])); - return $matches[1] ? substr($matches[0], 1) : ''.$whitespace; + return $matches[1] ? substr($matches[0], 1) : ''.$whitespace; }; return preg_replace_callback($pattern, $callback, $value); diff --git a/tests/View/ViewBladeCompilerTest.php b/tests/View/ViewBladeCompilerTest.php index caf93f1fe5f1..2b4b7aaf5b03 100644 --- a/tests/View/ViewBladeCompilerTest.php +++ b/tests/View/ViewBladeCompilerTest.php @@ -84,59 +84,59 @@ public function testEchosAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('', $compiler->compileString('{!!$name!!}')); - $this->assertEquals('', $compiler->compileString('{!! $name !!}')); - $this->assertEquals('', $compiler->compileString('{!! + $this->assertEquals('', $compiler->compileString('{!!$name!!}')); + $this->assertEquals('', $compiler->compileString('{!! $name !!}')); + $this->assertEquals('', $compiler->compileString('{!! $name !!}')); - $this->assertEquals('', $compiler->compileString('{!! $name or \'foo\' !!}')); + $this->assertEquals('', $compiler->compileString('{!! $name or \'foo\' !!}')); - $this->assertEquals('', $compiler->compileString('{{{$name}}}')); - $this->assertEquals('', $compiler->compileString('{{$name}}')); - $this->assertEquals('', $compiler->compileString('{{ $name }}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals('', $compiler->compileString('{{{$name}}}')); + $this->assertEquals('', $compiler->compileString('{{$name}}')); + $this->assertEquals('', $compiler->compileString('{{ $name }}')); + $this->assertEquals('', $compiler->compileString('{{ $name }}')); - $this->assertEquals("\n\n", $compiler->compileString("{{ \$name }}\n")); - $this->assertEquals("\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n")); - $this->assertEquals("\n\n", $compiler->compileString("{{ \$name }}\n")); - $this->assertEquals("\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n")); - - $this->assertEquals('', $compiler->compileString('{{ $name or "foo" }}')); - $this->assertEquals('name) ? $user->name : "foo"); ?>', $compiler->compileString('{{ $user->name or "foo" }}')); - $this->assertEquals('', $compiler->compileString('{{$name or "foo"}}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals("\n\n", $compiler->compileString("{{ \$name }}\n")); + $this->assertEquals("\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n")); + $this->assertEquals("\n\n", $compiler->compileString("{{ \$name }}\n")); + $this->assertEquals("\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n")); + + $this->assertEquals('', $compiler->compileString('{{ $name or "foo" }}')); + $this->assertEquals('name) ? $user->name : "foo"); ?>', $compiler->compileString('{{ $user->name or "foo" }}')); + $this->assertEquals('', $compiler->compileString('{{$name or "foo"}}')); + $this->assertEquals('', $compiler->compileString('{{ $name or "foo" }}')); - $this->assertEquals('', $compiler->compileString('{{ $name or \'foo\' }}')); - $this->assertEquals('', $compiler->compileString('{{$name or \'foo\'}}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals('', $compiler->compileString('{{ $name or \'foo\' }}')); + $this->assertEquals('', $compiler->compileString('{{$name or \'foo\'}}')); + $this->assertEquals('', $compiler->compileString('{{ $name or \'foo\' }}')); - $this->assertEquals('', $compiler->compileString('{{ $age or 90 }}')); - $this->assertEquals('', $compiler->compileString('{{$age or 90}}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals('', $compiler->compileString('{{ $age or 90 }}')); + $this->assertEquals('', $compiler->compileString('{{$age or 90}}')); + $this->assertEquals('', $compiler->compileString('{{ $age or 90 }}')); - $this->assertEquals('', $compiler->compileString('{{ "Hello world or foo" }}')); - $this->assertEquals('', $compiler->compileString('{{"Hello world or foo"}}')); - $this->assertEquals('', $compiler->compileString('{{$foo + $or + $baz}}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals('', $compiler->compileString('{{ "Hello world or foo" }}')); + $this->assertEquals('', $compiler->compileString('{{"Hello world or foo"}}')); + $this->assertEquals('', $compiler->compileString('{{$foo + $or + $baz}}')); + $this->assertEquals('', $compiler->compileString('{{ "Hello world or foo" }}')); - $this->assertEquals('', $compiler->compileString('{{ \'Hello world or foo\' }}')); - $this->assertEquals('', $compiler->compileString('{{\'Hello world or foo\'}}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals('', $compiler->compileString('{{ \'Hello world or foo\' }}')); + $this->assertEquals('', $compiler->compileString('{{\'Hello world or foo\'}}')); + $this->assertEquals('', $compiler->compileString('{{ \'Hello world or foo\' }}')); - $this->assertEquals('', $compiler->compileString('{{ myfunc(\'foo or bar\') }}')); - $this->assertEquals('', $compiler->compileString('{{ myfunc("foo or bar") }}')); - $this->assertEquals('', $compiler->compileString('{{ myfunc("$name or \'foo\'") }}')); + $this->assertEquals('', $compiler->compileString('{{ myfunc(\'foo or bar\') }}')); + $this->assertEquals('', $compiler->compileString('{{ myfunc("foo or bar") }}')); + $this->assertEquals('', $compiler->compileString('{{ myfunc("$name or \'foo\'") }}')); } @@ -163,10 +163,10 @@ public function testReversedEchosAreCompiled() $compiler = new BladeCompiler($this->getFiles(), __DIR__); $compiler->setEscapedContentTags('{{', '}}'); $compiler->setContentTags('{{{', '}}}'); - $this->assertEquals('', $compiler->compileString('{{$name}}')); - $this->assertEquals('', $compiler->compileString('{{{$name}}}')); - $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); - $this->assertEquals('', $compiler->compileString('{{{ + $this->assertEquals('', $compiler->compileString('{{$name}}')); + $this->assertEquals('', $compiler->compileString('{{{$name}}}')); + $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); + $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); } @@ -176,10 +176,10 @@ public function testShortRawEchosAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); $compiler->setRawTags('{{', '}}'); - $this->assertEquals('', $compiler->compileString('{{$name}}')); - $this->assertEquals('', $compiler->compileString('{{ $name }}')); - $this->assertEquals('', $compiler->compileString('{{{$name}}}')); - $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); + $this->assertEquals('', $compiler->compileString('{{$name}}')); + $this->assertEquals('', $compiler->compileString('{{ $name }}')); + $this->assertEquals('', $compiler->compileString('{{{$name}}}')); + $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); } @@ -188,13 +188,13 @@ public function testExtendsAreCompiled() $compiler = new BladeCompiler($this->getFiles(), __DIR__); $string = '@extends(\'foo\') test'; - $expected = "test".PHP_EOL.'make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>'; + $expected = "test".PHP_EOL.'make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>'; $this->assertEquals($expected, $compiler->compileString($string)); $compiler = new BladeCompiler($this->getFiles(), __DIR__); $string = '@extends(name(foo))'.PHP_EOL.'test'; - $expected = "test".PHP_EOL.'make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>'; + $expected = "test".PHP_EOL.'make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>'; $this->assertEquals($expected, $compiler->compileString($string)); } @@ -216,7 +216,7 @@ public function testStackIsCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); $string = '@stack(\'foo\')'; - $expected = 'yieldContent(\'foo\'); ?>'; + $expected = 'yieldContent(\'foo\'); ?>'; $this->assertEquals($expected, $compiler->compileString($string)); } @@ -437,7 +437,7 @@ public function testStatementThatContainsNonConsecutiveParanthesisAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); $string = "Foo @lang(function_call('foo(blah)')) bar"; - $expected = "Foo bar"; + $expected = "Foo bar"; $this->assertEquals($expected, $compiler->compileString($string)); } @@ -445,40 +445,40 @@ public function testStatementThatContainsNonConsecutiveParanthesisAreCompiled() public function testIncludesAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(\'foo\')')); - $this->assertEquals('make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(name(foo))')); + $this->assertEquals('make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(\'foo\')')); + $this->assertEquals('make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(name(foo))')); } public function testShowEachAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('renderEach(\'foo\', \'bar\'); ?>', $compiler->compileString('@each(\'foo\', \'bar\')')); - $this->assertEquals('renderEach(name(foo)); ?>', $compiler->compileString('@each(name(foo))')); + $this->assertEquals('renderEach(\'foo\', \'bar\'); ?>', $compiler->compileString('@each(\'foo\', \'bar\')')); + $this->assertEquals('renderEach(name(foo)); ?>', $compiler->compileString('@each(name(foo))')); } public function testYieldsAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('yieldContent(\'foo\'); ?>', $compiler->compileString('@yield(\'foo\')')); - $this->assertEquals('yieldContent(\'foo\', \'bar\'); ?>', $compiler->compileString('@yield(\'foo\', \'bar\')')); - $this->assertEquals('yieldContent(name(foo)); ?>', $compiler->compileString('@yield(name(foo))')); + $this->assertEquals('yieldContent(\'foo\'); ?>', $compiler->compileString('@yield(\'foo\')')); + $this->assertEquals('yieldContent(\'foo\', \'bar\'); ?>', $compiler->compileString('@yield(\'foo\', \'bar\')')); + $this->assertEquals('yieldContent(name(foo)); ?>', $compiler->compileString('@yield(name(foo))')); } public function testShowsAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('yieldSection(); ?>', $compiler->compileString('@show')); + $this->assertEquals('yieldSection(); ?>', $compiler->compileString('@show')); } public function testLanguageAndChoicesAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('', $compiler->compileString("@lang('foo')")); - $this->assertEquals('', $compiler->compileString("@choice('foo', 1)")); + $this->assertEquals('', $compiler->compileString("@lang('foo')")); + $this->assertEquals('', $compiler->compileString("@choice('foo', 1)")); } @@ -528,7 +528,7 @@ public function testCustomPhpCodeIsCorrectlyHandled() public function testMixingYieldAndEcho() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('yieldContent(\'title\'); ?> - ', $compiler->compileString("@yield('title') - {{Config::get('site.title')}}")); + $this->assertEquals('yieldContent(\'title\'); ?> - ', $compiler->compileString("@yield('title') - {{Config::get('site.title')}}")); } @@ -547,7 +547,7 @@ public function testCreateMatcher() function($view, BladeCompiler $compiler) { $pattern = $compiler->createMatcher('customControl'); - $replace = ''; + $replace = ''; return preg_replace($pattern, '$1'.$replace, $view); } ); @@ -556,7 +556,7 @@ function($view, BladeCompiler $compiler) @customControl(10, $foo, \'bar\') @endif'; $expected = ' - + '; $this->assertEquals($expected, $compiler->compileString($string)); } @@ -569,7 +569,7 @@ public function testCreatePlainMatcher() function($view, BladeCompiler $compiler) { $pattern = $compiler->createPlainMatcher('customControl'); - $replace = ''; + $replace = ''; return preg_replace($pattern, '$1'.$replace.'$2', $view); } ); @@ -578,7 +578,7 @@ function($view, BladeCompiler $compiler) @customControl @endif'; $expected = ' - + '; $this->assertEquals($expected, $compiler->compileString($string)); } @@ -590,9 +590,9 @@ public function testConfiguringContentTags() $compiler->setContentTags('[[', ']]'); $compiler->setEscapedContentTags('[[[', ']]]'); - $this->assertEquals('', $compiler->compileString('[[[ $name ]]]')); - $this->assertEquals('', $compiler->compileString('[[ $name ]]')); - $this->assertEquals('', $compiler->compileString('[[ + $this->assertEquals('', $compiler->compileString('[[[ $name ]]]')); + $this->assertEquals('', $compiler->compileString('[[ $name ]]')); + $this->assertEquals('', $compiler->compileString('[[ $name ]]')); } @@ -603,9 +603,9 @@ public function testRawTagsCanBeSetToLegacyValues() $compiler = new BladeCompiler($this->getFiles(), __DIR__); $compiler->setEchoFormat('%s'); - $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); - $this->assertEquals('', $compiler->compileString('{{ $name }}')); - $this->assertEquals('', $compiler->compileString('{{ + $this->assertEquals('', $compiler->compileString('{{{ $name }}}')); + $this->assertEquals('', $compiler->compileString('{{ $name }}')); + $this->assertEquals('', $compiler->compileString('{{ $name }}')); } @@ -614,16 +614,16 @@ public function testRawTagsCanBeSetToLegacyValues() public function testExpressionsOnTheSameLine() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals(' space () ', $compiler->compileString('@lang(foo(bar(baz(qux(breeze()))))) space () @lang(foo(bar))')); + $this->assertEquals(' space () ', $compiler->compileString('@lang(foo(bar(baz(qux(breeze()))))) space () @lang(foo(bar))')); } public function testExpressionWithinHTML() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals('>', $compiler->compileString('')); - $this->assertEquals('>', $compiler->compileString('')); - $this->assertEquals(' >', $compiler->compileString('')); + $this->assertEquals('>', $compiler->compileString('')); + $this->assertEquals('>', $compiler->compileString('')); + $this->assertEquals(' >', $compiler->compileString('')); } From 42f8f2dfacb9fa4d516e8cd2222a8159c862494b Mon Sep 17 00:00:00 2001 From: An Phan Date: Thu, 12 Mar 2015 00:12:13 +0800 Subject: [PATCH 3/3] Better use of inline string, single/double quotes, and sprinf --- .../View/Compilers/BladeCompiler.php | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index ad67a87c2bf0..2e6e6df01c6b 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -191,7 +191,7 @@ protected function compileExtensions($value) */ protected function compileComments($value) { - $pattern = sprintf('/%s--((.|\s)*?)--%s/', $this->contentTags[0], $this->contentTags[1]); + $pattern = "/{$this->contentTags[0]}--((.|\s)*?)--{$this->contentTags[1]}/"; return preg_replace($pattern, '', $value); } @@ -271,7 +271,7 @@ protected function compileStatements($value) */ protected function compileRawEchos($value) { - $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]); + $pattern = "/(@)?{$this->rawTags[0]}\s*(.+?)\s*{$this->rawTags[1]}(\r?\n)?/s"; $callback = function($matches) { @@ -291,7 +291,7 @@ protected function compileRawEchos($value) */ protected function compileRegularEchos($value) { - $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]); + $pattern = "/(@)?{$this->contentTags[0]}\s*(.+?)\s*{$this->contentTags[1]}(\r?\n)?/s"; $callback = function($matches) { @@ -313,7 +313,7 @@ protected function compileRegularEchos($value) */ protected function compileEscapedEchos($value) { - $pattern = sprintf('/%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]); + $pattern = "/{$this->escapedTags[0]}\s*(.+?)\s*{$this->escapedTags[1]}(\r?\n)?/s"; $callback = function($matches) { @@ -357,7 +357,7 @@ protected function compileInject($expression) { $segments = explode(',', preg_replace("/[\(\)\\\"\']/", '', $expression)); - return ""; + return '"; } /** @@ -456,7 +456,7 @@ protected function compileUnless($expression) */ protected function compileEndunless($expression) { - return ""; + return ''; } /** @@ -489,7 +489,7 @@ protected function compileChoice($expression) */ protected function compileElse($expression) { - return ""; + return ''; } /** @@ -524,7 +524,7 @@ protected function compileForelse($expression) { $empty = '$__empty_' . ++$this->forelseCounter; - return ""; + return ""; } /** @@ -559,7 +559,7 @@ protected function compileEmpty($expression) { $empty = '$__empty_' . $this->forelseCounter--; - return ""; + return ""; } /** @@ -581,7 +581,7 @@ protected function compileWhile($expression) */ protected function compileEndwhile($expression) { - return ""; + return ''; } /** @@ -592,7 +592,7 @@ protected function compileEndwhile($expression) */ protected function compileEndfor($expression) { - return ""; + return ''; } /** @@ -603,7 +603,7 @@ protected function compileEndfor($expression) */ protected function compileEndforeach($expression) { - return ""; + return ''; } /** @@ -614,7 +614,7 @@ protected function compileEndforeach($expression) */ protected function compileEndif($expression) { - return ""; + return ''; } /** @@ -625,7 +625,7 @@ protected function compileEndif($expression) */ protected function compileEndforelse($expression) { - return ""; + return ''; } /** @@ -716,7 +716,7 @@ public function extend(callable $compiler) */ public function createMatcher($function) { - return '/(?