Skip to content

Commit

Permalink
Merge pull request #9 from lotfio/analysis-ADgv6P
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
lotfio committed Sep 23, 2020
2 parents f086012 + 6a8ac0b commit 5d20554
Show file tree
Hide file tree
Showing 39 changed files with 358 additions and 318 deletions.
56 changes: 28 additions & 28 deletions src/Caprice.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,89 +15,89 @@
*/

use Caprice\Contracts\CapriceInterface;
use Caprice\Exception\CapriceException;

class Caprice implements CapriceInterface
{
{
/**
* caprice trait
* caprice trait.
*/
use CapriceTrait;

/**
* rules array
* rules array.
*
* @var array
*/
protected $rules;

/**
* parser
* parser.
*
* @var object
*/
protected $parser;

/**
* compile from directory
* compile from directory.
*
* @var string
* @var string
*/
protected $compileFromDir = './';

/**
* compile to directory
* compile to directory.
*
* @var string
* @var string
*/
protected $compileToDir = './';
protected $compileToDir = './';

/**
* recompile mode
* recompile mode.
*
* @var bool
* @var bool
*/
protected $recompile = false;

/**
* set up
* set up.
*/
public function __construct()
{
$this->rules = new CapriceRules;
$this->parser = new RuleParser;
$this->rules = new CapriceRules();
$this->parser = new RuleParser();
}

/**
* add directive method
* add directive method.
*
* @param string $directive
* @param mixed $callback
* @param bool $custom
* @return CapriceRules
* @param string $directive
* @param mixed $callback
* @param bool $custom
*
* @return CapriceRules
*/
public function directive(string $directive, $callback, $custom = false): CapriceRules
{
return $this->rules->add($directive, $callback, $custom);
}

/**
* compile cap file
* compile cap file.
*
* @param string $filename
*
* @param string $filename
*
* @return string
*/
public function compile(string $filename) : string
public function compile(string $filename): string
{
defined('COMPILE_FROM') || define('COMPILE_FROM', $this->compileFromDir);
defined('RE_COMPILE') || define('RE_COMPILE' , $this->recompile);
defined('RE_COMPILE') || define('RE_COMPILE', $this->recompile);

$compiler = new Compiler($this->parser, $this->rules);

return $compiler->compile(
$this->compileFromDir . dotPath($filename),
$this->compileFromDir.dotPath($filename),
$this->compileToDir
);
);
}
}
}
21 changes: 11 additions & 10 deletions src/CapriceRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class CapriceRules
{
/**
* caprice rules
* caprice rules.
*
* @var array
*/
Expand All @@ -26,29 +26,30 @@ class CapriceRules
];

/**
* add new directive rule
* add new directive rule.
*
* @param string $directive
* @param mixed $callback
*
* @param string $directive
* @param mixed $callback
* @return void
*/
public function add(string $directive, $callback, bool $custom) : self
public function add(string $directive, $callback, bool $custom): self
{
$this->rules[] = [
'directive' => $custom ? $directive : '~' . $directive . '(\s*\(((.*))\))?~',
'replace' => $callback
'directive' => $custom ? $directive : '~'.$directive.'(\s*\(((.*))\))?~',
'replace' => $callback,
];

return $this;
}

/**
* get available rules
* get available rules.
*
* @return array
*/
public function &getRules() : array
public function &getRules(): array
{
return $this->rules;
}
}
}
30 changes: 16 additions & 14 deletions src/CapriceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,49 @@
trait CapriceTrait
{
/**
* set compile locations
* set compile locations.
*
* @param string $compileFromDir
* @param string $compileToDir
*
* @param string $compileFromDir
* @param string $compileToDir
* @return Caprice
*/
public function setCompileLocations(string $compileFromDir, string $compileToDir): self
{
if(!is_dir($compileFromDir) || !is_writable($compileFromDir))
if (!is_dir($compileFromDir) || !is_writable($compileFromDir)) {
throw new CapriceException("input location $compileFromDir is not a valid writable directory.");

if(!is_dir($compileToDir) || !is_writable($compileToDir))
}
if (!is_dir($compileToDir) || !is_writable($compileToDir)) {
throw new CapriceException("input location $compileToDir is not a valid writable directory.");
}
$this->compileFromDir = rtrim($compileFromDir, '/').'/';
$this->compileToDir = rtrim($compileToDir, '/').'/';

$this->compileFromDir = rtrim($compileFromDir, '/') .'/';
$this->compileToDir = rtrim($compileToDir, '/') .'/';

return $this;
}

/**
* load predefined directives
* load predefined directives.
*
* @return Caprice
*/
public function loadPredefinedDirectives(): self
{
$caprice = $this;
require_once 'rules.php';

return $this;
}

/**
* enable recompile
* enable recompile.
*
* @return void
*/
public function enableRecompile(): self
{
$this->recompile = TRUE;
$this->recompile = true;

return $this;
}

}
}
57 changes: 31 additions & 26 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,73 +21,78 @@
class Compiler implements CompilerInterface
{
/**
* rules array
* rules array.
*
* @var array
*/
protected $rules;

/**
* parser
* parser.
*
* @var object
*/
protected $parser;

/**
* setup compiler
* setup compiler.
*
* @param RulesParserInterface $rules
*/
public function __construct(RuleParserInterface $parser, CapriceRules $rules)
{
$this->parser = $parser;
$this->rules = $rules;
$this->parser = $parser;
$this->rules = $rules;
}

/**
* check if file is modified
* check if file is modified.
*
* @param string $filename
* @return boolean
* @param string $filename
*
* @return bool
*/
protected function isModified(string $file, string $tempFile): bool
{
return !file_exists($tempFile) || filemtime($file) !== filemtime($tempFile);
}

/**
* compile caprice file
* compile caprice file.
*
* @param string $filename
* @param string $outputLocation
*
* @param string $filename
* @param string $outputLocation
*
* @return string
*/
public function compile(string $filename, string $outputLocation): string
{
if(!file_exists($filename))
if (!file_exists($filename)) {
throw new CapriceException("file $filename not found.");

}
// apply parsing to al rules
$rules = $this->rules->getRules();
$rules = $this->rules->getRules();

$content = \file_get_contents($filename);
$tempFile = $outputLocation . SHA1($filename) . '.php';
$content = \file_get_contents($filename);
$tempFile = $outputLocation.sha1($filename).'.php';

if(RE_COMPILE || $this->isModified($filename, $tempFile)) // if cap file is modified or doesn't exists
{
for($i = 0; $i < count($rules); $i++)
foreach($rules as $rule)
if (RE_COMPILE || $this->isModified($filename, $tempFile)) { // if cap file is modified or doesn't exists
for ($i = 0; $i < count($rules); $i++) {
foreach ($rules as $rule) {
$content = $this->parser->parse($content, $rule);
}
}

//save file
if(file_put_contents($tempFile, trim($content)))
touch($filename); touch($tempFile);
//save file
if (file_put_contents($tempFile, trim($content))) {
touch($filename);
}
touch($tempFile);
}
if(!file_exists($tempFile))

if (!file_exists($tempFile)) {
throw new CapriceException("error compiling, file $tempFile not found.");
}

return $tempFile;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Contracts/CapriceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
interface CapriceInterface
{
/**
* add a directive method
* add a directive method.
*
* @param string $directive
* @param mixed $callback
*
* @param string $directive
* @param mixed $callback
* @return CapriceRules
*/
public function directive(string $directive, $callback): CapriceRules;
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/CompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ interface CompilerInterface
*
* This method gets a string file, compiles it
* and generates a PHP file based on the .cap file
*
* @param string $file cap file
*
* @param string $file cap file
* @param string output location
*
* @return string
Expand Down
11 changes: 6 additions & 5 deletions src/Contracts/DirectiveInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
*
*/

interface DirectiveInterface
interface DirectiveInterface
{
/**
* directive interface
* directive interface.
*
* @param string $expression
* @param string|null $file
*
* @param string $expression
* @param string|null $file
* @return string
*/
public function replace(string $expression, ?string $file = null) : string;
public function replace(string $expression, ?string $file = null): string;
}
Loading

0 comments on commit 5d20554

Please sign in to comment.