From b6b883841b5c9d8b97f4a356d16754a841769f6f Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 14:51:19 +0100 Subject: [PATCH 01/17] update readme --- README.md | 69 +++---------------------------------------------------- 1 file changed, 3 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 08c2349..4fe9eb7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ ### :fire: Introduction : Caprice is PHP templating engine that aims to write clean PHP syntax along side with HTML code. -caprice compiles the syntax and generate php files which means no performance loss but a clean html files with a friendly syntax. +caprice compiles the syntax and generate php files which means no performance loss but a clean html files with a friendly syntax. ### :pushpin: Requirements : - PHP 7.2 or newer versions @@ -43,75 +43,12 @@ caprice compiles the syntax and generate php files which means no performance lo $compiled = $compiler->compile("test.cap.php"); // file to compile - require $compiled; // require your compiled file + require $compiled; // require your compiled file ``` ### :inbox_tray: Available syntax directives: -```cpp - // code blocks - (( echo "hello" )) - - (( function test(){ return "test";} )) - - // echo statment - (- "hello caprice" -) - - // echo escaped statement - (= "hello caprice" =) - - // array access statement - $variable.key - - // if statement - #if ($condition) - // logic - #elif ($condition2) - // elseif logic - #else - // else logic - #endif - - // for in loop key only - #for ($name in $names) - (- $name -) - #endfor - - // for in loop key + value - #for ($name => $age in $names) - (- $name . "=>" . $age -) - #endfor - - // for loop - #for ($i = 0; $i <= 10; $i++) - (- $i . "
" -) - #endfor - - // while loop - #while (TRUE) - // do something - #endwhile - - // include/require statments - // you can remove .cap.php extension for both - // you use . to access folder instead of / - #require("file.cap.php") - #include("file.cap.php") - - // extends a base layout - #extends("layout.cap.php") - // load a section - #yield("sectionName") - - // define a section - #section("sectionName") - // section content - #endsection - - // functions - // dump - #dump($variable) OR #dd($variable) +- check the documentation here [ChangeLog](docs/examples.md). -``` ### :helicopter: TODO - Adding support for custom directives. From e0fef88569258b4aaac5192c0975a8279dae0f2c Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 14:52:56 +0100 Subject: [PATCH 02/17] remove unused method --- src/Compiler.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/Compiler.php b/src/Compiler.php index 1d1bab0..aa236b6 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -117,22 +117,6 @@ public function setProductionMode() : bool return $this->productionMode = true; } - /** - * check if caprice file eis modified. - * - * @param string $file - * @param string $cached - * - * @return bool - */ - public function isModified(string $file, string $cached) : bool - { - $template = filemtime($file); - $generated = @filemtime($cached); // just ignore and generate a file if no file exists - - return $template !== $generated; - } - /** * remove extra lines on a file. * From 6488d05a54f021e0d5a387b1706b966d89fd911e Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 14:54:47 +0100 Subject: [PATCH 03/17] remove is modified method tests --- tests/Unit/CompilerTest.php | 11 ----------- ...e2e77.php => 288c6dee42a0ff50458b2a74664cb858.php} | 0 2 files changed, 11 deletions(-) rename tests/stub/{18d315750fea6c67f481285ab3ae2e77.php => 288c6dee42a0ff50458b2a74664cb858.php} (100%) diff --git a/tests/Unit/CompilerTest.php b/tests/Unit/CompilerTest.php index f9a73c8..92187a3 100644 --- a/tests/Unit/CompilerTest.php +++ b/tests/Unit/CompilerTest.php @@ -64,17 +64,6 @@ public function testCompileCacheDirNotExists() $this->compiler = new Compiler(dirname(__DIR__).'/stub', 'cachedir'); } - /** - * test is modified is returning bool. - * - * @return void - */ - public function testIsModifiedMethod() - { - $check = $this->compiler->isModified(dirname(__DIR__).'/stub/fileOne', dirname(__DIR__).'/stub/fileTwo'); - $this->assertIsBool($check); - } - /** * test compile cap file. * diff --git a/tests/stub/18d315750fea6c67f481285ab3ae2e77.php b/tests/stub/288c6dee42a0ff50458b2a74664cb858.php similarity index 100% rename from tests/stub/18d315750fea6c67f481285ab3ae2e77.php rename to tests/stub/288c6dee42a0ff50458b2a74664cb858.php From bc672fe21717904c1a68ddeaf497fe97cc355d44 Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 15:23:12 +0100 Subject: [PATCH 04/17] fix to allow allow all chars for second variable (.,->,@) --- src/Directives/ForInValueOnlyStatement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Directives/ForInValueOnlyStatement.php b/src/Directives/ForInValueOnlyStatement.php index bdd7715..d512363 100644 --- a/src/Directives/ForInValueOnlyStatement.php +++ b/src/Directives/ForInValueOnlyStatement.php @@ -23,7 +23,7 @@ class ForInValueOnlyStatement implements DirectiveInterface * * @var string */ - public $pattern = '/#for\s*\((\$\w+\s*)(\s*in\s*)(\$\w+\s*)\)(.*?)#endfor/s'; + public $pattern = '/#for\s*\((\$\w+\s*)(\s*in\s*)(.*)\)(.*?)#endfor/s'; /** * directive replace method. From 8b56cf18255ad05b3fc6778c6e704eb6840fe3bc Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 15:28:50 +0100 Subject: [PATCH 05/17] adding remove Htmlcomment directive --- src/Directives/HtmlCommentStatement.php | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Directives/HtmlCommentStatement.php diff --git a/src/Directives/HtmlCommentStatement.php b/src/Directives/HtmlCommentStatement.php new file mode 100644 index 0000000..d2b3368 --- /dev/null +++ b/src/Directives/HtmlCommentStatement.php @@ -0,0 +1,41 @@ + + * @copyright Lotfio Lakehal 2019 + * @license MIT + * @link https://github.com/lotfio/caprice + * + */ + +use Caprice\Contracts\DirectiveInterface; + +class HtmlCommentStatement implements DirectiveInterface +{ + /** + * pattern property. + * + * @var string + */ + public $pattern = '//s'; + + /** + * directive replace method. + * + * @param array $match + * @param string $file original file + * @param string $filesDir .cap files dir + * + * @return string + */ + public function replace(array $match, string $file, string $filesDir) : string + { + return ''; + } +} From 784c9ea9df1f4ddbced96443e0accba3c9ec3e68 Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 15:30:00 +0100 Subject: [PATCH 06/17] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fe9eb7..c1a6729 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ caprice compiles the syntax and generate php files which means no performance lo $compiler = new Caprice\Compiler("filesDirectory", "cacheDirectory"); // if production do not forget to enable production mode - //$comoiler->setProductionMode(); + //$compiler->setProductionMode(); $compiled = $compiler->compile("test.cap.php"); // file to compile From 04fa1abf4f124aad3c963013f0ad2b4a9e2ce01e Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 15:32:19 +0100 Subject: [PATCH 07/17] fix link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c1a6729..a8656f2 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ caprice compiles the syntax and generate php files which means no performance lo ``` ### :inbox_tray: Available syntax directives: -- check the documentation here [ChangeLog](docs/examples.md). +- check the documentation here [Docs](https://github.com/lotfio/caprice/blob/master/docs/exapmles.md). ### :helicopter: TODO - Adding support for custom directives. From 90d1d300991519ddd8b39b35898ae1354a6ffd98 Mon Sep 17 00:00:00 2001 From: lotfio Date: Thu, 6 Feb 2020 15:41:58 +0100 Subject: [PATCH 08/17] update for in regex --- src/Directives/ForInStatement.php | 2 +- src/Directives/ForInValueOnlyStatement.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Directives/ForInStatement.php b/src/Directives/ForInStatement.php index 207b100..8a4b55a 100644 --- a/src/Directives/ForInStatement.php +++ b/src/Directives/ForInStatement.php @@ -23,7 +23,7 @@ class ForInStatement implements DirectiveInterface * * @var string */ - public $pattern = '/#for\s*\((\$\w+\s*=>\s*\$\w+\s*)(\s*in\s*)(\$\w+\s*)\)(.*?)#endfor/s'; + public $pattern = '/#for\s*\((\$\w+\s*=>\s*\$\w+\s*)(\s*in\s*)(.*?)\)(.*?)#endfor/s'; /** * directive replace method. diff --git a/src/Directives/ForInValueOnlyStatement.php b/src/Directives/ForInValueOnlyStatement.php index d512363..53e47d7 100644 --- a/src/Directives/ForInValueOnlyStatement.php +++ b/src/Directives/ForInValueOnlyStatement.php @@ -23,7 +23,7 @@ class ForInValueOnlyStatement implements DirectiveInterface * * @var string */ - public $pattern = '/#for\s*\((\$\w+\s*)(\s*in\s*)(.*)\)(.*?)#endfor/s'; + public $pattern = '/#for\s*\((\$\w+\s*)(\s*in\s*)(.*?)\)(.*?)#endfor/s'; /** * directive replace method. From 39dd8a8d6a102954e06952f322334d0ee220ef23 Mon Sep 17 00:00:00 2001 From: lotfio Date: Sun, 9 Feb 2020 15:46:46 +0100 Subject: [PATCH 09/17] adding ability to add custom directives --- README.md | 3 ++ docs/exapmles.md | 12 ++++---- src/Compiler.php | 41 ++++++++++++++------------- src/Parser.php | 24 ++++++++++++---- src/Utils.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a8656f2..175ae2f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ caprice compiles the syntax and generate php files which means no performance lo // if production do not forget to enable production mode //$compiler->setProductionMode(); + // add custom directives + //$compiler->extendDirectives($dir); + $compiled = $compiler->compile("test.cap.php"); // file to compile require $compiled; // require your compiled file diff --git a/docs/exapmles.md b/docs/exapmles.md index 7c07853..9560dc2 100644 --- a/docs/exapmles.md +++ b/docs/exapmles.md @@ -2,7 +2,7 @@ - you can write any php inside code blocks ```js - (( + (( $var1 = "foo"; $var2 = "bar"; echo $var1 . " and " . $var2; @@ -36,7 +36,7 @@ // logic #endif ``` -- if else +- if else ```js // if statement #if ($condition) @@ -45,7 +45,7 @@ // else logic #endif ``` -- if elseif +- if elseif ```js #if ($condiftion) // iflogic @@ -53,7 +53,7 @@ #elif ($condition2) // elseif logic - #else + #else // else logic #endif ``` @@ -104,10 +104,10 @@ # layout directives ```js - // extends a base layout + // extends a base layout // here we are extending master.cap.php from layouts folder #extends("layouts.master") - // load a section + // load a section #yield("sectionName") // define a section diff --git a/src/Compiler.php b/src/Compiler.php index aa236b6..a7492d0 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -48,6 +48,13 @@ class Compiler implements CompilerInterface */ private $productionMode = false; + /** + * extended directives + * + * @var array + */ + private $extendedDirectives = NULL; + /** * compiler constructor. * @@ -67,6 +74,18 @@ public function __construct(string $filesDir, string $cacheDir) $this->cacheDir = rtrim(rtrim($cacheDir, '\\'), '/').DIRECTORY_SEPARATOR; } + + /** + * set extended directives + * + * @param string $dir + * @return void + */ + public function extendDirectives(string $dir) : void + { + $this->extendedDirectives = $dir; + } + /** * compile caprice file method. * @@ -82,26 +101,20 @@ public function compile(string $fileName) : string if (!file_exists($capFile)) { throw new FileNotFoundException("file $capFile not found", 4); } - //cache file $cacheFile = $this->cacheDir.md5($capFile).'.php'; - // create cache file if not exists to prevent filemtime check error - if (!file_exists($cacheFile)) { - touch($cacheFile); - } if ($this->productionMode == false) { // if development recompile // read caprice file $this->file = file_get_contents($capFile); // parse caprice file - $parser = new Parser($this->filesDir); + $parser = new Parser($this->filesDir, $this->extendedDirectives); for ($i = 0; $i <= 6; $i++) { // loop to parse several times (necessary to parse extends and includes) $this->file = $parser->parseFile($this->file); } - file_put_contents($cacheFile, $this->removeExtraLines($this->file)); - touch($capFile, time()); // update caprice time to be the same as cahed file to detect any changes later + file_put_contents($cacheFile, Utils::removeExtraLines($this->file)); } return $cacheFile; @@ -116,16 +129,4 @@ public function setProductionMode() : bool { return $this->productionMode = true; } - - /** - * remove extra lines on a file. - * - * @param string $file - * - * @return void - */ - public function removeExtraLines(string $file) : string - { - return preg_replace("~[\r\n]+~", "\r\n", trim($file)); //remove extra lines - } } diff --git a/src/Parser.php b/src/Parser.php index defdd0b..e6b0f50 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -19,6 +19,15 @@ class Parser implements ParserInterface { + /** + * directives to apply for parsing + * + * @var array + */ + private $directives = array( + + ); + /** * base files directory. * @@ -38,9 +47,13 @@ class Parser implements ParserInterface * * @param string $filesDir */ - public function __construct(string $filesDir) + public function __construct(string $filesDir, $extendDirectives = NULL) { - $this->filesDir = $filesDir; + $this->filesDir = $filesDir; + + $this->directives = Utils::scanForDirectives(__DIR__ . '/Directives'); + if(!is_null($extendDirectives)) + $this->directives = array_merge($this->directives, Utils::scanForDirectives($extendDirectives)); } /** @@ -72,10 +85,9 @@ public function parseFile(string $file) : string { $this->file = $file; - foreach (glob(__DIR__.'/Directives/*.php') as $class) { - $class = trim((explode('Directives', $class)[1]), '/'); - $class = 'Caprice\\Directives\\'.ucfirst($class); - $class = substr($class, 0, strpos($class, '.php')); + foreach ($this->directives as $class) { + + $class = trim($class, '::class'); if (class_exists($class)) { $this->file = $this->parse(new $class(), $this->file); diff --git a/src/Utils.php b/src/Utils.php index a773ffe..8b1adbe 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -13,6 +13,7 @@ * @link https://github.com/lotfio/caprice * */ +use Caprice\Exception\FileNotFoundException; class Utils { @@ -27,4 +28,76 @@ public static function hideSections(string $file) : string { return preg_replace('/#section\s*\((.*?)\)(.*?)#endsection/s', null, $file); } + + /** + * remove extra lines on a file. + * + * @param string $file + * + * @return void + */ + public static function removeExtraLines(string $file) : string + { + return preg_replace("~[\r\n]+~", "\r\n", trim($file)); //remove extra lines + } + + /** + * extract php file namespace + * + * @param string $file + * @return ?string + */ + public static function getNamespace(string $file) : ?string + { + $ns = NULL; + + if(!file_exists($file)) + throw new FileNotFoundException("can scan for namespace file not found ", 4); + $handle = fopen($file, "r"); + if ($handle) { + while (($line = fgets($handle)) !== false) { + if (strpos($line, 'namespace') === 0) { + $parts = explode(' ', $line); + $ns = rtrim(trim($parts[1]), ';'); + break; + } + } + fclose($handle); + } + return $ns; + } + + /** + * scan for directives method + * + * @param string $directory + * @param string $namespace + * @return array + */ + public static function scanForDirectives(string $directory, $namespace = NULL) : array + { + // directives + $directives = array(); + + // scan directives folder + $dir = scandir($directory); + $dir = array_filter($dir, function($elem){ + return ($elem != '.' && $elem != '..' && strpos($elem, '.php') !== false) ? $elem : NULL; + }); + $dir = array_values($dir); + + if (count($dir) > 0) { + + // get namespace for first or use default namespace + $namespace = (is_null($namespace)) ? self::getNamespace(rtrim($directory,'/') . '/' . $dir[0]) : $namespace; + + // mach all directives to their namespace + foreach ($dir as $directive) { + $directives[] = $namespace . '\\' . ucfirst(rtrim($directive, '.php')) . "::class"; + } + } + // return an associative array of directives with the namespace + return $directives; + + } } From e0ac3e547f07316b68d559927443564851c7adc7 Mon Sep 17 00:00:00 2001 From: lotfio Date: Mon, 10 Feb 2020 09:07:35 +0100 Subject: [PATCH 10/17] adding tests for utils helper class --- src/Utils.php | 7 +- tests/Unit/ParserTest.php | 12 ++ tests/Unit/UtilsTest.php | 124 ++++++++++++++++++ ...p => 61fda84888811ec003ad730119c18d18.php} | 0 tests/stub/directives/.gitkeep | 0 tests/stub/namespace.php | 3 + 6 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/UtilsTest.php rename tests/stub/{288c6dee42a0ff50458b2a74664cb858.php => 61fda84888811ec003ad730119c18d18.php} (100%) create mode 100644 tests/stub/directives/.gitkeep create mode 100644 tests/stub/namespace.php diff --git a/src/Utils.php b/src/Utils.php index 8b1adbe..1cfc89b 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -14,6 +14,7 @@ * */ use Caprice\Exception\FileNotFoundException; +use Caprice\Exception\DirNotFoundException; class Utils { @@ -52,7 +53,7 @@ public static function getNamespace(string $file) : ?string $ns = NULL; if(!file_exists($file)) - throw new FileNotFoundException("can scan for namespace file not found ", 4); + throw new FileNotFoundException("can not scan for namespace file not found ", 4); $handle = fopen($file, "r"); if ($handle) { while (($line = fgets($handle)) !== false) { @@ -79,6 +80,8 @@ public static function scanForDirectives(string $directory, $namespace = NULL) : // directives $directives = array(); + if(!is_dir($directory)) + throw new DirNotFoundException("directory $directory not found ", 4); // scan directives folder $dir = scandir($directory); $dir = array_filter($dir, function($elem){ @@ -93,7 +96,7 @@ public static function scanForDirectives(string $directory, $namespace = NULL) : // mach all directives to their namespace foreach ($dir as $directive) { - $directives[] = $namespace . '\\' . ucfirst(rtrim($directive, '.php')) . "::class"; + $directives[] = $namespace . '\\' . ucfirst(str_replace('.php', NULL, $directive)) . "::class"; } } // return an associative array of directives with the namespace diff --git a/tests/Unit/ParserTest.php b/tests/Unit/ParserTest.php index 2f96ccc..ad61f48 100644 --- a/tests/Unit/ParserTest.php +++ b/tests/Unit/ParserTest.php @@ -271,4 +271,16 @@ public function testDumpStatementFound() $string = '#dump ($var) #dd($var)'; $this->assertSame(' ', $this->parser->parse($directive, $string)); } + + /** + * test dump found statement. + * + * @return void + */ + public function testRemoveHtmlComment() + { + $directive = new Directives\HtmlCommentStatement(); + $string = ''; + $this->assertSame('', $this->parser->parse($directive, $string)); + } } diff --git a/tests/Unit/UtilsTest.php b/tests/Unit/UtilsTest.php new file mode 100644 index 0000000..384bfde --- /dev/null +++ b/tests/Unit/UtilsTest.php @@ -0,0 +1,124 @@ + + * @copyright Lotfio Lakehal 2019 + * @license MIT + * @link https://github.com/lotfio/caprice + * + */ + +use Caprice\Utils; +use PHPUnit\Framework\TestCase; +use Caprice\Exception\FileNotFoundException; +use Caprice\Exception\DirNotFoundException; + +class UtilsTest extends TestCase +{ + /** + * assert hide sections works + * + * @return void + */ + public function testHideSectionsMethod() + { + $string = "#section('test') + section content here + #endsection"; + + $parse = Utils::hideSections($string); + $this->assertEmpty($parse); + } + + /** + * assert remove extra lines + * + * @return void + */ + public function testRemoveExtraLines() + { + $string = "line 1 + + line 3"; + + $parse = Utils::removeExtraLines($string); + + $expected = "line 1 + line 3"; + $this->assertSame($expected, $parse); + } + + /** + * test get namespace file not noud + * + * @return void + */ + public function testGetNamespaceFileNotFound() + { + $this->expectException(FileNotFoundException::class); + $this->assertNull(Utils::getNamespace(dirname(__DIR__).'/stub/nofile')); + } + + /** + * test get namespace on file that doesn't have a namespace + * + * @return void + */ + public function testGetNamespaceReturnNull() + { + $this->assertNull(Utils::getNamespace(dirname(__DIR__).'/stub/test.cap.php')); + } + + /** + * test get namespace correct namespace + * + * @return void + */ + public function testGetNamespaceReturnNamespace() + { + $this->assertEquals('Caprice\TestGetNamespace', Utils::getNamespace(dirname(__DIR__).'/stub/namespace.php')); + } + + /** + * test scanForDirectives when directory not exists + * + * @return void + */ + public function testScanForDirectivesNotFoundDirectory() + { + $this->expectException(DirNotFoundException::class); + Utils::scanForDirectives(dirname(__DIR__).'/directives'); + } + + /** + * test scanForDirectives on empty dir + * + * @return void + */ + public function testScanForDirectivesEmptyDir() + { + $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub/directives'); + $this->assertEmpty($dvs); + } + + /** + * test scanForDirectives + * + * @return void + */ + public function testScanForDirectives() + { + $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub'); + $this->assertEquals('\Namespace::class', $dvs[2]); + // custom namespace + $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub', 'customNameSpace'); + $this->assertEquals('customNameSpace\Namespace::class', $dvs[2]); + } + +} \ No newline at end of file diff --git a/tests/stub/288c6dee42a0ff50458b2a74664cb858.php b/tests/stub/61fda84888811ec003ad730119c18d18.php similarity index 100% rename from tests/stub/288c6dee42a0ff50458b2a74664cb858.php rename to tests/stub/61fda84888811ec003ad730119c18d18.php diff --git a/tests/stub/directives/.gitkeep b/tests/stub/directives/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/stub/namespace.php b/tests/stub/namespace.php new file mode 100644 index 0000000..5c92011 --- /dev/null +++ b/tests/stub/namespace.php @@ -0,0 +1,3 @@ + Date: Mon, 10 Feb 2020 10:11:09 +0100 Subject: [PATCH 11/17] fix class name trim --- src/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index e6b0f50..b496cfd 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -87,7 +87,7 @@ public function parseFile(string $file) : string foreach ($this->directives as $class) { - $class = trim($class, '::class'); + $class = rtrim($class, '::class'); if (class_exists($class)) { $this->file = $this->parse(new $class(), $this->file); From 6d1f788c14e7674a70d47ba599ad66c7dbef2eb2 Mon Sep 17 00:00:00 2001 From: lotfio lakehal Date: Mon, 10 Feb 2020 15:38:12 +0100 Subject: [PATCH 12/17] Update CompilerInterface.php --- src/Contracts/CompilerInterface.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Contracts/CompilerInterface.php b/src/Contracts/CompilerInterface.php index cd5ae4b..98aaea7 100644 --- a/src/Contracts/CompilerInterface.php +++ b/src/Contracts/CompilerInterface.php @@ -19,13 +19,10 @@ interface CompilerInterface /** * compile method. * - * This method gets a parsed string from parsed file - * and it compiles it and generates a PHP file based - * on the .cap file - * This compile method generates a file only if the .cap - * file is modified + * This method gets a string file, compiles it + * and generates a PHP file based on the .cap file * - * @return void + * @return string */ public function compile(string $file) : string; } From 8c0b430731762ca3092973f01a2da79bad16b4d5 Mon Sep 17 00:00:00 2001 From: lotfio lakehal Date: Mon, 10 Feb 2020 21:12:36 +0000 Subject: [PATCH 13/17] Apply fixes from StyleCI --- src/Compiler.php | 16 ++++---- src/Contracts/CompilerInterface.php | 4 +- src/Contracts/DirectiveInterface.php | 2 +- src/Contracts/ParserInterface.php | 4 +- src/Directives/ArrayAccessStatement.php | 2 +- src/Directives/BreakStatement.php | 2 +- src/Directives/CodeBlock.php | 2 +- src/Directives/ContinueStatement.php | 2 +- src/Directives/DumpStatement.php | 2 +- src/Directives/EchoEscapedStatement.php | 2 +- src/Directives/EchoStatement.php | 2 +- src/Directives/ElseIfStatement.php | 2 +- src/Directives/ElseStatement.php | 2 +- src/Directives/EndIfStatement.php | 2 +- src/Directives/ExtendsStatement.php | 2 +- src/Directives/ForInStatement.php | 2 +- src/Directives/ForInValueOnlyStatement.php | 2 +- src/Directives/ForLoop.php | 2 +- src/Directives/HtmlCommentStatement.php | 2 +- src/Directives/IfStatement.php | 2 +- src/Directives/IncludeStatement.php | 2 +- src/Directives/WhileLoop.php | 2 +- src/Directives/YieldStatement.php | 2 +- src/Parser.php | 20 +++++----- src/Utils.php | 46 ++++++++++++---------- src/helpers.php | 2 +- tests/Unit/CompilerTest.php | 2 +- tests/Unit/ParserTest.php | 2 +- tests/Unit/UtilsTest.php | 33 ++++++++-------- tests/stub/ns/namespace.php | 2 +- 30 files changed, 87 insertions(+), 84 deletions(-) diff --git a/src/Compiler.php b/src/Compiler.php index cf16d42..99bde10 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -49,11 +49,11 @@ class Compiler implements CompilerInterface private $productionMode = false; /** - * extended directives + * extended directives. * * @var array */ - private $extendedDirectives = NULL; + private $extendedDirectives = null; /** * compiler constructor. @@ -74,16 +74,16 @@ public function __construct(string $filesDir, string $cacheDir) $this->cacheDir = rtrim(rtrim($cacheDir, '\\'), '/').DIRECTORY_SEPARATOR; } - /** - * set extended directives + * set extended directives. * * @param string $dir + * * @return void */ - public function extendDirectives(string $dir) : void + public function extendDirectives(string $dir): void { - $this->extendedDirectives = $dir; + $this->extendedDirectives = $dir; } /** @@ -94,7 +94,7 @@ public function extendDirectives(string $dir) : void * * @return string compiled file */ - public function compile(string $fileName) : string + public function compile(string $fileName): string { $capFile = $this->filesDir.$fileName; @@ -125,7 +125,7 @@ public function compile(string $fileName) : string * * @return void */ - public function setProductionMode() : bool + public function setProductionMode(): bool { return $this->productionMode = true; } diff --git a/src/Contracts/CompilerInterface.php b/src/Contracts/CompilerInterface.php index a54e9da..9e89baf 100644 --- a/src/Contracts/CompilerInterface.php +++ b/src/Contracts/CompilerInterface.php @@ -19,10 +19,10 @@ interface CompilerInterface /** * compile method. * - * This method gets a string file, compiles it + * This method gets a string file, compiles it * and generates a PHP file based on the .cap file * * @return string */ - public function compile(string $file) : string; + public function compile(string $file): string; } diff --git a/src/Contracts/DirectiveInterface.php b/src/Contracts/DirectiveInterface.php index 40e5c54..3ef3deb 100644 --- a/src/Contracts/DirectiveInterface.php +++ b/src/Contracts/DirectiveInterface.php @@ -28,5 +28,5 @@ interface DirectiveInterface * * @return void */ - public function replace(array $match, string $file, string $filesDir) : string; + public function replace(array $match, string $file, string $filesDir): string; } diff --git a/src/Contracts/ParserInterface.php b/src/Contracts/ParserInterface.php index dcd5874..afe178f 100644 --- a/src/Contracts/ParserInterface.php +++ b/src/Contracts/ParserInterface.php @@ -29,7 +29,7 @@ interface ParserInterface * * @return void */ - public function parse(DirectiveInterface $directive, string $file) : string; + public function parse(DirectiveInterface $directive, string $file): string; /** * parse file method. @@ -41,5 +41,5 @@ public function parse(DirectiveInterface $directive, string $file) : string; * * @return string */ - public function parseFile(string $file) : string; + public function parseFile(string $file): string; } diff --git a/src/Directives/ArrayAccessStatement.php b/src/Directives/ArrayAccessStatement.php index e6cc50f..475e6f5 100644 --- a/src/Directives/ArrayAccessStatement.php +++ b/src/Directives/ArrayAccessStatement.php @@ -34,7 +34,7 @@ class ArrayAccessStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return $match[2].'["'.$match[3].'"]'.$match[4]; } diff --git a/src/Directives/BreakStatement.php b/src/Directives/BreakStatement.php index dcab629..369e4fa 100644 --- a/src/Directives/BreakStatement.php +++ b/src/Directives/BreakStatement.php @@ -34,7 +34,7 @@ class BreakStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/CodeBlock.php b/src/Directives/CodeBlock.php index 419d9af..1a92633 100644 --- a/src/Directives/CodeBlock.php +++ b/src/Directives/CodeBlock.php @@ -34,7 +34,7 @@ class CodeBlock implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/ContinueStatement.php b/src/Directives/ContinueStatement.php index 4a80eb9..9a46d94 100644 --- a/src/Directives/ContinueStatement.php +++ b/src/Directives/ContinueStatement.php @@ -34,7 +34,7 @@ class ContinueStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/DumpStatement.php b/src/Directives/DumpStatement.php index d5425c6..1423c76 100644 --- a/src/Directives/DumpStatement.php +++ b/src/Directives/DumpStatement.php @@ -34,7 +34,7 @@ class DumpStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/EchoEscapedStatement.php b/src/Directives/EchoEscapedStatement.php index 7a68b4b..55af896 100644 --- a/src/Directives/EchoEscapedStatement.php +++ b/src/Directives/EchoEscapedStatement.php @@ -34,7 +34,7 @@ class EchoEscapedStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/EchoStatement.php b/src/Directives/EchoStatement.php index 7545b3b..97e2163 100644 --- a/src/Directives/EchoStatement.php +++ b/src/Directives/EchoStatement.php @@ -34,7 +34,7 @@ class EchoStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/ElseIfStatement.php b/src/Directives/ElseIfStatement.php index de631d2..9a764b6 100644 --- a/src/Directives/ElseIfStatement.php +++ b/src/Directives/ElseIfStatement.php @@ -34,7 +34,7 @@ class ElseIfStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/ElseStatement.php b/src/Directives/ElseStatement.php index 6dfd02e..f60239e 100644 --- a/src/Directives/ElseStatement.php +++ b/src/Directives/ElseStatement.php @@ -34,7 +34,7 @@ class ElseStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/EndIfStatement.php b/src/Directives/EndIfStatement.php index 2ba0ffd..05cb871 100644 --- a/src/Directives/EndIfStatement.php +++ b/src/Directives/EndIfStatement.php @@ -34,7 +34,7 @@ class EndIfStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/ExtendsStatement.php b/src/Directives/ExtendsStatement.php index 6755737..b1eb903 100644 --- a/src/Directives/ExtendsStatement.php +++ b/src/Directives/ExtendsStatement.php @@ -35,7 +35,7 @@ class ExtendsStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { $content = $filesDir.dotPath($match[1]); diff --git a/src/Directives/ForInStatement.php b/src/Directives/ForInStatement.php index 2f826be..9eac3f5 100644 --- a/src/Directives/ForInStatement.php +++ b/src/Directives/ForInStatement.php @@ -34,7 +34,7 @@ class ForInStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''.trim($match[4]).''; } diff --git a/src/Directives/ForInValueOnlyStatement.php b/src/Directives/ForInValueOnlyStatement.php index 6e14d30..c9f4679 100644 --- a/src/Directives/ForInValueOnlyStatement.php +++ b/src/Directives/ForInValueOnlyStatement.php @@ -34,7 +34,7 @@ class ForInValueOnlyStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''.trim($match[4]).''; } diff --git a/src/Directives/ForLoop.php b/src/Directives/ForLoop.php index 07b881b..49c2870 100644 --- a/src/Directives/ForLoop.php +++ b/src/Directives/ForLoop.php @@ -34,7 +34,7 @@ class ForLoop implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''.trim($match[4]).''; } diff --git a/src/Directives/HtmlCommentStatement.php b/src/Directives/HtmlCommentStatement.php index 31cf079..9fe1fac 100644 --- a/src/Directives/HtmlCommentStatement.php +++ b/src/Directives/HtmlCommentStatement.php @@ -34,7 +34,7 @@ class HtmlCommentStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/IfStatement.php b/src/Directives/IfStatement.php index 63cfa16..46d354c 100644 --- a/src/Directives/IfStatement.php +++ b/src/Directives/IfStatement.php @@ -34,7 +34,7 @@ class IfStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''; } diff --git a/src/Directives/IncludeStatement.php b/src/Directives/IncludeStatement.php index 2ccf357..9cf8529 100644 --- a/src/Directives/IncludeStatement.php +++ b/src/Directives/IncludeStatement.php @@ -35,7 +35,7 @@ class IncludeStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { $file = $filesDir.dotPath($match[2]); diff --git a/src/Directives/WhileLoop.php b/src/Directives/WhileLoop.php index 13078ed..7c42b46 100644 --- a/src/Directives/WhileLoop.php +++ b/src/Directives/WhileLoop.php @@ -34,7 +34,7 @@ class WhileLoop implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { return ''.trim($match[2]).''; } diff --git a/src/Directives/YieldStatement.php b/src/Directives/YieldStatement.php index 251c7da..a556540 100644 --- a/src/Directives/YieldStatement.php +++ b/src/Directives/YieldStatement.php @@ -34,7 +34,7 @@ class YieldStatement implements DirectiveInterface * * @return string */ - public function replace(array $match, string $file, string $filesDir) : string + public function replace(array $match, string $file, string $filesDir): string { $secPattern = '/#section\s*\(('.trim($match[1]).')\)(.*?)#endsection/s'; diff --git a/src/Parser.php b/src/Parser.php index dcdfd5a..02b2995 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -20,13 +20,13 @@ class Parser implements ParserInterface { /** - * directives to apply for parsing + * directives to apply for parsing. * * @var array */ - private $directives = array( + private $directives = [ - ); + ]; /** * base files directory. @@ -47,13 +47,14 @@ class Parser implements ParserInterface * * @param string $filesDir */ - public function __construct(string $filesDir, $extendDirectives = NULL) + public function __construct(string $filesDir, $extendDirectives = null) { - $this->filesDir = $filesDir; + $this->filesDir = $filesDir; - $this->directives = Utils::scanForDirectives(__DIR__ . '/Directives'); - if(!is_null($extendDirectives)) + $this->directives = Utils::scanForDirectives(__DIR__.'/Directives'); + if (!is_null($extendDirectives)) { $this->directives = array_merge($this->directives, Utils::scanForDirectives($extendDirectives)); + } } /** @@ -64,7 +65,7 @@ public function __construct(string $filesDir, $extendDirectives = NULL) * * @return void */ - public function parse(DirectiveInterface $directive, string $file) : string + public function parse(DirectiveInterface $directive, string $file): string { return preg_replace_callback($directive->pattern, function (array $match) use ($directive, $file) { @@ -81,12 +82,11 @@ public function parse(DirectiveInterface $directive, string $file) : string * * @return void */ - public function parseFile(string $file) : string + public function parseFile(string $file): string { $this->file = $file; foreach ($this->directives as $class) { - $class = rtrim($class, '::class'); if (class_exists($class)) { diff --git a/src/Utils.php b/src/Utils.php index 290576f..8148adc 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -13,8 +13,8 @@ * @link https://github.com/lotfio/caprice * */ -use Caprice\Exception\FileNotFoundException; use Caprice\Exception\DirNotFoundException; +use Caprice\Exception\FileNotFoundException; class Utils { @@ -25,7 +25,7 @@ class Utils * * @return string */ - public static function hideSections(string $file) : string + public static function hideSections(string $file): string { return preg_replace('/#section\s*\((.*?)\)(.*?)#endsection/s', null, $file); } @@ -37,24 +37,26 @@ public static function hideSections(string $file) : string * * @return void */ - public static function removeExtraLines(string $file) : string + public static function removeExtraLines(string $file): string { return preg_replace("~[\r\n]+~", "\r\n", trim($file)); //remove extra lines } /** - * extract php file namespace + * extract php file namespace. + * + * @param string $file * - * @param string $file * @return ?string */ - public static function getNamespace(string $file) : ?string + public static function getNamespace(string $file): ?string { - $ns = NULL; + $ns = null; - if(!file_exists($file)) - throw new FileNotFoundException("can not scan for namespace file not found ", 4); - $handle = fopen($file, "r"); + if (!file_exists($file)) { + throw new FileNotFoundException('can not scan for namespace file not found ', 4); + } + $handle = fopen($file, 'r'); if ($handle) { while (($line = fgets($handle)) !== false) { if (strpos($line, 'namespace') === 0) { @@ -65,42 +67,44 @@ public static function getNamespace(string $file) : ?string } fclose($handle); } + return $ns; } /** - * scan for directives method + * scan for directives method. + * + * @param string $directory + * @param string $namespace * - * @param string $directory - * @param string $namespace * @return array */ - public static function scanForDirectives(string $directory, $namespace = NULL) : array + public static function scanForDirectives(string $directory, $namespace = null): array { // directives - $directives = array(); + $directives = []; - if(!is_dir($directory)) + if (!is_dir($directory)) { throw new DirNotFoundException("directory $directory not found ", 4); + } // scan directives folder $dir = scandir($directory); - $dir = array_filter($dir, function($elem){ - return ($elem != '.' && $elem != '..' && strpos($elem, '.php') !== false) ? $elem : NULL; + $dir = array_filter($dir, function ($elem) { + return ($elem != '.' && $elem != '..' && strpos($elem, '.php') !== false) ? $elem : null; }); $dir = array_values($dir); if (count($dir) > 0) { // get namespace for first or use default namespace - $namespace = (is_null($namespace)) ? self::getNamespace(rtrim($directory,'/') . '/' . $dir[0]) : $namespace; + $namespace = (is_null($namespace)) ? self::getNamespace(rtrim($directory, '/').'/'.$dir[0]) : $namespace; // mach all directives to their namespace foreach ($dir as $directive) { - $directives[] = $namespace . '\\' . ucfirst(str_replace('.php', NULL, $directive)) . "::class"; + $directives[] = $namespace.'\\'.ucfirst(str_replace('.php', null, $directive)).'::class'; } } // return an associative array of directives with the namespace return $directives; - } } diff --git a/src/helpers.php b/src/helpers.php index 8fe8cf0..5d716c4 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -22,7 +22,7 @@ * * @return string */ - function dotPath(string $filePath) : string + function dotPath(string $filePath): string { $path = preg_replace('/\'|"/', null, $filePath); $path = preg_replace('/(.php|.cap.php)/', null, $path); diff --git a/tests/Unit/CompilerTest.php b/tests/Unit/CompilerTest.php index 92187a3..f835f55 100644 --- a/tests/Unit/CompilerTest.php +++ b/tests/Unit/CompilerTest.php @@ -26,7 +26,7 @@ class CompilerTest extends TestCase * * @return void */ - public function setUp() : void + public function setUp(): void { $this->compiler = new Compiler(dirname(__DIR__).'/stub/', dirname(__DIR__).'/stub/'); } diff --git a/tests/Unit/ParserTest.php b/tests/Unit/ParserTest.php index ad61f48..26de6c6 100644 --- a/tests/Unit/ParserTest.php +++ b/tests/Unit/ParserTest.php @@ -25,7 +25,7 @@ class ParserTest extends TestCase * * @return void */ - public function setUp() : void + public function setUp(): void { $this->parser = new Parser(dirname(__DIR__).'/stub/'); } diff --git a/tests/Unit/UtilsTest.php b/tests/Unit/UtilsTest.php index 5bebc8d..c3d4099 100644 --- a/tests/Unit/UtilsTest.php +++ b/tests/Unit/UtilsTest.php @@ -14,41 +14,41 @@ * */ +use Caprice\Exception\DirNotFoundException; +use Caprice\Exception\FileNotFoundException; use Caprice\Utils; use PHPUnit\Framework\TestCase; -use Caprice\Exception\FileNotFoundException; -use Caprice\Exception\DirNotFoundException; class UtilsTest extends TestCase { /** - * assert hide sections works + * assert hide sections works. * * @return void */ public function testHideSectionsMethod() { - $string = "#section('test') + $string = "#section('test') section content here #endsection"; - $parse = Utils::hideSections($string); + $parse = Utils::hideSections($string); $this->assertEmpty($parse); } /** - * assert remove extra lines + * assert remove extra lines. * * @return void */ public function testRemoveExtraLines() { - $parse = Utils::removeExtraLines("\n"); - $this->assertSame("", $parse); + $parse = Utils::removeExtraLines("\n"); + $this->assertSame('', $parse); } - /** - * test get namespace file not noud + /** + * test get namespace file not noud. * * @return void */ @@ -59,7 +59,7 @@ public function testGetNamespaceFileNotFound() } /** - * test get namespace on file that doesn't have a namespace + * test get namespace on file that doesn't have a namespace. * * @return void */ @@ -69,7 +69,7 @@ public function testGetNamespaceReturnNull() } /** - * test get namespace correct namespace + * test get namespace correct namespace. * * @return void */ @@ -79,7 +79,7 @@ public function testGetNamespaceReturnNamespace() } /** - * test scanForDirectives when directory not exists + * test scanForDirectives when directory not exists. * * @return void */ @@ -90,7 +90,7 @@ public function testScanForDirectivesNotFoundDirectory() } /** - * test scanForDirectives on empty dir + * test scanForDirectives on empty dir. * * @return void */ @@ -101,7 +101,7 @@ public function testScanForDirectivesEmptyDir() } /** - * test scanForDirectives + * test scanForDirectives. * * @return void */ @@ -113,5 +113,4 @@ public function testScanForDirectives() $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub/ns', 'customNameSpace'); $this->assertEquals('customNameSpace\Namespace::class', $dvs[1]); } - -} \ No newline at end of file +} diff --git a/tests/stub/ns/namespace.php b/tests/stub/ns/namespace.php index 5c92011..48b5f2c 100644 --- a/tests/stub/ns/namespace.php +++ b/tests/stub/ns/namespace.php @@ -1,3 +1,3 @@ Date: Mon, 10 Feb 2020 21:51:38 -0800 Subject: [PATCH 14/17] update tests --- tests/Unit/UtilsTest.php | 10 +++++----- .../NoNamespace.php} | 0 tests/stub/{ => ns}/namespace.php | 0 3 files changed, 5 insertions(+), 5 deletions(-) rename tests/stub/{61fda84888811ec003ad730119c18d18.php => ns/NoNamespace.php} (100%) rename tests/stub/{ => ns}/namespace.php (100%) diff --git a/tests/Unit/UtilsTest.php b/tests/Unit/UtilsTest.php index 384bfde..273600c 100644 --- a/tests/Unit/UtilsTest.php +++ b/tests/Unit/UtilsTest.php @@ -82,7 +82,7 @@ public function testGetNamespaceReturnNull() */ public function testGetNamespaceReturnNamespace() { - $this->assertEquals('Caprice\TestGetNamespace', Utils::getNamespace(dirname(__DIR__).'/stub/namespace.php')); + $this->assertEquals('Caprice\TestGetNamespace', Utils::getNamespace(dirname(__DIR__).'/stub/ns/namespace.php')); } /** @@ -114,11 +114,11 @@ public function testScanForDirectivesEmptyDir() */ public function testScanForDirectives() { - $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub'); - $this->assertEquals('\Namespace::class', $dvs[2]); + $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub/ns'); + $this->assertEquals('\NoNamespace::class', $dvs[0]); // custom namespace - $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub', 'customNameSpace'); - $this->assertEquals('customNameSpace\Namespace::class', $dvs[2]); + $dvs = Utils::scanForDirectives(dirname(__DIR__).'/stub/ns', 'customNameSpace'); + $this->assertEquals('customNameSpace\Namespace::class', $dvs[1]); } } \ No newline at end of file diff --git a/tests/stub/61fda84888811ec003ad730119c18d18.php b/tests/stub/ns/NoNamespace.php similarity index 100% rename from tests/stub/61fda84888811ec003ad730119c18d18.php rename to tests/stub/ns/NoNamespace.php diff --git a/tests/stub/namespace.php b/tests/stub/ns/namespace.php similarity index 100% rename from tests/stub/namespace.php rename to tests/stub/ns/namespace.php From 914c99a2163085c47ca2ff9709ba3d574b32f995 Mon Sep 17 00:00:00 2001 From: lotfio Date: Mon, 10 Feb 2020 21:56:05 -0800 Subject: [PATCH 15/17] fix test --- tests/Unit/UtilsTest.php | 13 ++++++++----- tests/stub/40a81f8b9bf8684945389100d3866c0c.php | 0 2 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 tests/stub/40a81f8b9bf8684945389100d3866c0c.php diff --git a/tests/Unit/UtilsTest.php b/tests/Unit/UtilsTest.php index 273600c..c54fd41 100644 --- a/tests/Unit/UtilsTest.php +++ b/tests/Unit/UtilsTest.php @@ -43,14 +43,17 @@ public function testHideSectionsMethod() */ public function testRemoveExtraLines() { - $string = "line 1 - - line 3"; + $string = <<assertSame($expected, $parse); } diff --git a/tests/stub/40a81f8b9bf8684945389100d3866c0c.php b/tests/stub/40a81f8b9bf8684945389100d3866c0c.php new file mode 100644 index 0000000..e69de29 From d051be1c62dfc21bdcddbec58252504c15465992 Mon Sep 17 00:00:00 2001 From: lotfio Date: Mon, 10 Feb 2020 22:03:15 -0800 Subject: [PATCH 16/17] fix test remove new line --- tests/Unit/UtilsTest.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tests/Unit/UtilsTest.php b/tests/Unit/UtilsTest.php index c54fd41..5bebc8d 100644 --- a/tests/Unit/UtilsTest.php +++ b/tests/Unit/UtilsTest.php @@ -43,18 +43,8 @@ public function testHideSectionsMethod() */ public function testRemoveExtraLines() { - $string = <<assertSame($expected, $parse); + $parse = Utils::removeExtraLines("\n"); + $this->assertSame("", $parse); } /** From c2c93929f3381e4c462ecd072ff840fd28e314db Mon Sep 17 00:00:00 2001 From: lotfio Date: Mon, 10 Feb 2020 22:11:27 -0800 Subject: [PATCH 17/17] update fr v0.3.0 --- CHANGELOG.md | 7 ++++++- README.md | 2 +- src/Compiler.php | 2 +- src/Contracts/CompilerInterface.php | 2 +- src/Contracts/DirectiveInterface.php | 2 +- src/Contracts/ParserInterface.php | 2 +- src/Directives/ArrayAccessStatement.php | 2 +- src/Directives/BreakStatement.php | 2 +- src/Directives/CodeBlock.php | 2 +- src/Directives/ContinueStatement.php | 2 +- src/Directives/DumpStatement.php | 2 +- src/Directives/EchoEscapedStatement.php | 2 +- src/Directives/EchoStatement.php | 2 +- src/Directives/ElseIfStatement.php | 2 +- src/Directives/ElseStatement.php | 2 +- src/Directives/EndIfStatement.php | 2 +- src/Directives/ExtendsStatement.php | 2 +- src/Directives/ForInStatement.php | 2 +- src/Directives/ForInValueOnlyStatement.php | 2 +- src/Directives/ForLoop.php | 2 +- src/Directives/HtmlCommentStatement.php | 2 +- src/Directives/IfStatement.php | 2 +- src/Directives/IncludeStatement.php | 2 +- src/Directives/WhileLoop.php | 2 +- src/Directives/YieldStatement.php | 2 +- src/Exception/DirNotFoundException.php | 2 +- src/Exception/FileNotFoundException.php | 2 +- src/Parser.php | 2 +- src/Utils.php | 2 +- src/helpers.php | 2 +- 30 files changed, 35 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 047eecf..f80803a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,4 +6,9 @@ ## 0.2.0 - adding new directives (#extends, #yield, #section, #dd, #dump) - adding environment ($compiler->setProductionMode()) -- adding Utils and helpers \ No newline at end of file +- adding Utils and helpers + +## 0.3.0 +- ability to add custom directives ($compiler->extendDirectives($dir)) +- fix dot notaion array access on for in loops +- no error when sections not found \ No newline at end of file diff --git a/README.md b/README.md index 175ae2f..4087171 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

License PHP version - Version + Version Coverage Build Status StyleCi diff --git a/src/Compiler.php b/src/Compiler.php index a7492d0..cf16d42 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Contracts/CompilerInterface.php b/src/Contracts/CompilerInterface.php index 98aaea7..a54e9da 100644 --- a/src/Contracts/CompilerInterface.php +++ b/src/Contracts/CompilerInterface.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Contracts/DirectiveInterface.php b/src/Contracts/DirectiveInterface.php index dcdb116..40e5c54 100644 --- a/src/Contracts/DirectiveInterface.php +++ b/src/Contracts/DirectiveInterface.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Contracts/ParserInterface.php b/src/Contracts/ParserInterface.php index c0d954b..dcd5874 100644 --- a/src/Contracts/ParserInterface.php +++ b/src/Contracts/ParserInterface.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ArrayAccessStatement.php b/src/Directives/ArrayAccessStatement.php index 97271dd..e6cc50f 100644 --- a/src/Directives/ArrayAccessStatement.php +++ b/src/Directives/ArrayAccessStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/BreakStatement.php b/src/Directives/BreakStatement.php index 82031d7..dcab629 100644 --- a/src/Directives/BreakStatement.php +++ b/src/Directives/BreakStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/CodeBlock.php b/src/Directives/CodeBlock.php index bcfbe8a..419d9af 100644 --- a/src/Directives/CodeBlock.php +++ b/src/Directives/CodeBlock.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ContinueStatement.php b/src/Directives/ContinueStatement.php index b5fb726..4a80eb9 100644 --- a/src/Directives/ContinueStatement.php +++ b/src/Directives/ContinueStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/DumpStatement.php b/src/Directives/DumpStatement.php index 92128ba..d5425c6 100644 --- a/src/Directives/DumpStatement.php +++ b/src/Directives/DumpStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/EchoEscapedStatement.php b/src/Directives/EchoEscapedStatement.php index 8033a11..7a68b4b 100644 --- a/src/Directives/EchoEscapedStatement.php +++ b/src/Directives/EchoEscapedStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/EchoStatement.php b/src/Directives/EchoStatement.php index 92b8d14..7545b3b 100644 --- a/src/Directives/EchoStatement.php +++ b/src/Directives/EchoStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ElseIfStatement.php b/src/Directives/ElseIfStatement.php index 165d461..de631d2 100644 --- a/src/Directives/ElseIfStatement.php +++ b/src/Directives/ElseIfStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ElseStatement.php b/src/Directives/ElseStatement.php index c60c7d0..6dfd02e 100644 --- a/src/Directives/ElseStatement.php +++ b/src/Directives/ElseStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/EndIfStatement.php b/src/Directives/EndIfStatement.php index fc07941..2ba0ffd 100644 --- a/src/Directives/EndIfStatement.php +++ b/src/Directives/EndIfStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ExtendsStatement.php b/src/Directives/ExtendsStatement.php index d92c531..6755737 100644 --- a/src/Directives/ExtendsStatement.php +++ b/src/Directives/ExtendsStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ForInStatement.php b/src/Directives/ForInStatement.php index 8a4b55a..2f826be 100644 --- a/src/Directives/ForInStatement.php +++ b/src/Directives/ForInStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ForInValueOnlyStatement.php b/src/Directives/ForInValueOnlyStatement.php index 53e47d7..6e14d30 100644 --- a/src/Directives/ForInValueOnlyStatement.php +++ b/src/Directives/ForInValueOnlyStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/ForLoop.php b/src/Directives/ForLoop.php index f11c239..07b881b 100644 --- a/src/Directives/ForLoop.php +++ b/src/Directives/ForLoop.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/HtmlCommentStatement.php b/src/Directives/HtmlCommentStatement.php index d2b3368..31cf079 100644 --- a/src/Directives/HtmlCommentStatement.php +++ b/src/Directives/HtmlCommentStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/IfStatement.php b/src/Directives/IfStatement.php index a5d5e43..63cfa16 100644 --- a/src/Directives/IfStatement.php +++ b/src/Directives/IfStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/IncludeStatement.php b/src/Directives/IncludeStatement.php index ee4995c..2ccf357 100644 --- a/src/Directives/IncludeStatement.php +++ b/src/Directives/IncludeStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/WhileLoop.php b/src/Directives/WhileLoop.php index 0c3cf8a..13078ed 100644 --- a/src/Directives/WhileLoop.php +++ b/src/Directives/WhileLoop.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Directives/YieldStatement.php b/src/Directives/YieldStatement.php index df9caf8..251c7da 100644 --- a/src/Directives/YieldStatement.php +++ b/src/Directives/YieldStatement.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Exception/DirNotFoundException.php b/src/Exception/DirNotFoundException.php index d81147f..c2647cb 100644 --- a/src/Exception/DirNotFoundException.php +++ b/src/Exception/DirNotFoundException.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Exception/FileNotFoundException.php b/src/Exception/FileNotFoundException.php index 23eb7fc..d3a8a2c 100644 --- a/src/Exception/FileNotFoundException.php +++ b/src/Exception/FileNotFoundException.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Parser.php b/src/Parser.php index b496cfd..dcdfd5a 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/Utils.php b/src/Utils.php index 1cfc89b..290576f 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -6,7 +6,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT diff --git a/src/helpers.php b/src/helpers.php index ce4318b..8fe8cf0 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,7 +4,7 @@ * This file is a part of Caprice package * * @package Caprice - * @version 0.2.0 + * @version 0.3.0 * @author Lotfio Lakehal * @copyright Lotfio Lakehal 2019 * @license MIT