Skip to content

Commit

Permalink
Add methods:
Browse files Browse the repository at this point in the history
- disallowJadeFile
- allowJadeFile
- isJadeFileAllowed
  • Loading branch information
kylekatarnls committed Jun 4, 2018
1 parent e17a3a1 commit ec1f1c3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -251,3 +251,20 @@ $this->settings([
'view_path' => APPPATH . 'pug-templates'
]);
```

## Disallow `.jade` legacy file extension

If you use only up-to-date `.pug` file extension, you can disallow the `.jade` extension
to avoid unnecessary file existence checks:

```php
class Pug_Controller extends CI_Controller {
use CiPug;

public function __construct()
{
parent::__construct();
$this->disallowJadeFile();
}
}
```
72 changes: 66 additions & 6 deletions src/CiJade.php
Expand Up @@ -5,9 +5,55 @@
*/
trait CiJade
{
/**
* @var \Pug\Pug|null
*/
protected $jade;

/**
* @var string|null
*/
protected $jade_view_path;

/**
* @var bool
*/
protected $allow_jade_file = true;

/**
* Allow legacy .jade file extension. (Reduce performances).
*
* @return $this
*/
public function allowJadeFile()
{
$this->allow_jade_file = true;

return $this;
}

/**
* Disallow legacy .jade file extension. (Improve performances).
*
* @return $this
*/
public function disallowJadeFile()
{
$this->allow_jade_file = false;

return $this;
}

/**
* Returns true if legacy .jade file extension is allowed.
*
* @return bool
*/
public function isJadeFileAllowed()
{
return $this->allow_jade_file;
}

/**
* Extract view path from the options or return the default app view path.
*
Expand Down Expand Up @@ -81,6 +127,24 @@ public function settings(array $options = null)
return $this;
}

/**
* Returns a jade file path if it match the given view path without extension.
*
* @param string $view
*
* @return string
*/
private function lookForJadeFile($view)
{
$view .= '.jade';
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -11)) === '/index.jade');
$view = $isIndex ? substr($view, 0, -11).'.jade' : substr($view, 0, -5).DIRECTORY_SEPARATOR.'index.jade';
}

return $view;
}

/**
* Returns the matching path of a given view name.
*
Expand All @@ -94,12 +158,8 @@ public function getViewPath($view)
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -10)) === '/index.pug');
$view = $isIndex ? substr($view, 0, -10).'.pug' : substr($view, 0, -4).DIRECTORY_SEPARATOR.'index.pug';
if (!file_exists($view)) {
$view = substr($view, 0, $isIndex ? -4 : -10).'.jade';
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -11)) === '/index.jade');
$view = $isIndex ? substr($view, 0, -11).'.jade' : substr($view, 0, -5).DIRECTORY_SEPARATOR.'index.jade';
}
if ($this->isJadeFileAllowed() && !file_exists($view)) {
$view = $this->lookForJadeFile(substr($view, 0, $isIndex ? -4 : -10));
}
}

Expand Down
28 changes: 28 additions & 0 deletions tests/CiPugTest.php
Expand Up @@ -140,4 +140,32 @@ public function testSettings()
}
rmdir($directory);
}

public function testLegacyJadeFileExtension()
{
$controller = new Controller();
$html = $controller->view('foo/bar', true);

$this->assertSame('<div><span></span></div>', $html);

$controller = new Controller();
$controller->disallowJadeFile();
$message = '';

try {
$controller->view('foo/bar', true);
} catch (\Phug\CompilerException $exp) {
$message = $exp->getMessage();
}

$this->assertRegExp('/foo[\\\\\\/]bar[\\\\\\/]index\.pug not found/', $message);

$controller = new Controller();
$controller->allowJadeFile();
$html = $controller->view('foo/bar', true);

$this->assertSame('<div><span></span></div>', $html);

$controller->disallowJadeFile();
}
}

0 comments on commit ec1f1c3

Please sign in to comment.