Skip to content

Commit

Permalink
Merge pull request #4 from lotfio/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
lotfio authored Feb 10, 2020
2 parents 766cb99 + 92d0aa0 commit 3b23435
Show file tree
Hide file tree
Showing 38 changed files with 372 additions and 192 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
## 0.2.0
- adding new directives (#extends, #yield, #section, #dd, #dump)
- adding environment ($compiler->setProductionMode())
- adding Utils and helpers
- 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
76 changes: 8 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p align="center">
<img src="https://img.shields.io/badge/Licence-MIT-ffd32a.svg" alt="License">
<img src="https://img.shields.io/badge/PHP-7.2-808e9b.svg" alt="PHP version">
<img src="https://img.shields.io/badge/Version-0.2.0-f53b57.svg" alt="Version">
<img src="https://img.shields.io/badge/Version-0.3.0-f53b57.svg" alt="Version">
<img src="https://img.shields.io/badge/coverage-10%25-27ae60.svg" alt="Coverage">
<img src="https://travis-ci.org/lotfio/caprice.svg?branch=master" alt="Build Status">
<img src="https://github.styleci.io/repos/211069554/shield?branch=master" alt="StyleCi">
Expand All @@ -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
Expand All @@ -39,79 +39,19 @@ 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();

// add custom directives
//$compiler->extendDirectives($dir);

$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 . "<br>" -)
#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 [Docs](https://github.com/lotfio/caprice/blob/master/docs/exapmles.md).

```
### :helicopter: TODO
- Adding support for custom directives.

Expand Down
12 changes: 6 additions & 6 deletions docs/exapmles.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- you can write any php inside code blocks

```js
((
((
$var1 = "foo";
$var2 = "bar";
echo $var1 . " and " . $var2;
Expand Down Expand Up @@ -36,7 +36,7 @@
// logic
#endif
```
- if else
- if else
```js
// if statement
#if ($condition)
Expand All @@ -45,15 +45,15 @@
// else logic
#endif
```
- if elseif
- if elseif
```js
#if ($condiftion)
// iflogic

#elif ($condition2)

// elseif logic
#else
#else
// else logic
#endif
```
Expand Down Expand Up @@ -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
Expand Down
63 changes: 24 additions & 39 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact@lotfio.net>
* @copyright Lotfio Lakehal 2019
* @license MIT
Expand Down Expand Up @@ -48,6 +48,13 @@ class Compiler implements CompilerInterface
*/
private $productionMode = false;

/**
* extended directives.
*
* @var array
*/
private $extendedDirectives = null;

/**
* compiler constructor.
*
Expand All @@ -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.
*
Expand All @@ -75,33 +94,27 @@ public function __construct(string $filesDir, string $cacheDir)
*
* @return string compiled file
*/
public function compile(string $fileName) : string
public function compile(string $fileName): string
{
$capFile = $this->filesDir.$fileName;

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;
Expand All @@ -112,36 +125,8 @@ public function compile(string $fileName) : string
*
* @return void
*/
public function setProductionMode() : bool
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.
*
* @param string $file
*
* @return void
*/
public function removeExtraLines(string $file) : string
{
return preg_replace("~[\r\n]+~", "\r\n", trim($file)); //remove extra lines
}
}
13 changes: 5 additions & 8 deletions src/Contracts/CompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact@lotfio.net>
* @copyright Lotfio Lakehal 2019
* @license MIT
Expand All @@ -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;
public function compile(string $file): string;
}
4 changes: 2 additions & 2 deletions src/Contracts/DirectiveInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact@lotfio.net>
* @copyright Lotfio Lakehal 2019
* @license MIT
Expand All @@ -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;
}
6 changes: 3 additions & 3 deletions src/Contracts/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact@lotfio.net>
* @copyright Lotfio Lakehal 2019
* @license MIT
Expand All @@ -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.
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions src/Directives/ArrayAccessStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact@lotfio.net>
* @copyright Lotfio Lakehal 2019
* @license MIT
Expand Down Expand Up @@ -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];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Directives/BreakStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact@lotfio.net>
* @copyright Lotfio Lakehal 2019
* @license MIT
Expand Down Expand Up @@ -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 '<?php break;?>';
}
Expand Down
Loading

0 comments on commit 3b23435

Please sign in to comment.