Skip to content

Commit

Permalink
Add own cache for same behavior on production and development mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Matějček committed Apr 18, 2017
1 parent 548f676 commit c773c41
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 52 deletions.
12 changes: 2 additions & 10 deletions README.md
Expand Up @@ -13,11 +13,9 @@ extensions:
assetsExtension: h4kuna\Assets\DI\AssetsExtension

assetsExtension:
version: 2017-04-17 # YYYY-MM-DD # any production flag, like last day or build number

# optional
wwwDir: %wwwDir% # Where is your www dir?
debugMode: %debugMode% # If you need production behavior.
tempDir: %tempDir% # If you want change temp dir.
```
How to use in latte. After install extension, you have available filter **asset**.

Expand All @@ -29,18 +27,12 @@ How to use in latte. After install extension, you have available filter **asset*
<script src="{='js/main.js'|asset}"></script>
```

Output with development mode looks like ``?file mtime``.
Output looks like ``?file mtime``.
```html
<link rel="stylesheet" href="/css/main.css?123456">
<script src="/js/main.js?456789"></script>
```

Output with production mode looks like.
```html
<link rel="stylesheet" href="/css/main.css?2017-04-17">
<script src="/js/main.js?2017-04-17"></script>
```

Absolute path is posible with double slash.
```html
<link rel="stylesheet" href="{='//css/main.css'|asset}">
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -12,9 +12,10 @@
}
],
"require": {
"nette/bootstrap": "~2.0",
"latte/latte": "~2.0",
"nette/application": "~2.0",
"latte/latte": "~2.0"
"nette/bootstrap": "~2.0",
"nette/utils": "~2.0"
},
"autoload": {
"psr-4": {
Expand Down
68 changes: 68 additions & 0 deletions src/CacheAssets.php
@@ -0,0 +1,68 @@
<?php

namespace h4kuna\Assets;

use Nette\Utils;

class CacheAssets
{

/** @var string */
private $tempFile;

/** @var int[] */
private $files;

/** @var bool */
private $save = FALSE;

public function __construct($tempDir)
{
Utils\FileSystem::createDir($tempDir);
$this->tempFile = $tempDir . DIRECTORY_SEPARATOR . '_assets';
}

/**
* @param string $pathname
* @return int|NULL
*/
public function load($pathname)
{
$this->loadCache();
if (isset($this->files[$pathname])) {
return $this->files[$pathname];
}

return $this->save($pathname);
}

private function loadCache()
{
if ($this->files !== NULL) {
return;
}

$this->files = array();
if (is_file($this->tempFile)) {
$this->files = require $this->tempFile;
}
}

private function save($pathname)
{
$mtime = filemtime($pathname);
$this->files[$pathname] = $mtime;
if ($this->save === FALSE) {
$this->save = TRUE;
}
return $mtime;
}

public function __destruct()
{
if ($this->save === TRUE) {
file_put_contents($this->tempFile, '<?php return ' . var_export($this->files, TRUE) . ';');
}
}

}
18 changes: 6 additions & 12 deletions src/DI/AssetsExtension.php
Expand Up @@ -4,33 +4,27 @@

use Nette\DI as NDI;

/**
* Use in template
* {='css/final.css'|asset}
*/
class AssetsExtension extends \Nette\DI\CompilerExtension
{

private $defaults = array(
'version' => '1.0',
'wwwDir' => '%wwwDir%',
'debugMode' => '%debugMode%'
'tempDir' => '%tempDir%'
);

public function loadConfiguration()
{
$config = $this->getConfig($this->defaults);
$builder = $this->getContainerBuilder();
$assetFile = $builder->addDefinition($this->prefix('file'))
->setClass('h4kuna\\Assets\\File', array($config['wwwDir']))
->addSetup('setDebugMode', array($config['debugMode']))
->addSetup('setVersion', array($config['version']));

$cache = $builder->addDefinition($this->prefix('cache'))
->setClass('h4kuna\\Assets\\CacheAssets', array($config['tempDir']));

$assetFile = $builder->addDefinition($this->prefix('file'))
->setClass('h4kuna\\Assets\\File', array($config['wwwDir'], $cache));

$builder->getDefinition('latte.latteFactory')
->addSetup('addFilter', array('asset', new NDI\Statement("array(?, 'createUrl')", array($assetFile))));
}

}


31 changes: 6 additions & 25 deletions src/File.php
Expand Up @@ -10,49 +10,30 @@ class File
/** @var string */
private $rootFs;

/** @var CacheAssets */
private $cache;

/** @var Http\Url */
private $url;

/** @var bool */
private $debugMode = FALSE;

/** @var string */
private $version;

public function __construct($rootFs, Http\Request $request)
public function __construct($rootFs, CacheAssets $cache, Http\Request $request)
{
$this->rootFs = $rootFs;
$this->cache = $cache;
$this->url = $request->getUrl();
}

public function setDebugMode($debugMode)
{
$this->debugMode = (bool) $debugMode;
}

public function setVersion($version)
{
$this->version = $version;
}

public function createUrl($file)
{
$fsPath = $this->rootFs . DIRECTORY_SEPARATOR . $file;
if ($this->debugMode) {
$postfix = filemtime($fsPath);
} elseif ($this->version) {
$postfix = $this->version;
} else {
$postfix = date('Y-m-d');
}

$host = $this->url->getBasePath();
if (substr($file, 0, 2) == '//') {
$host = $this->url->getHostUrl() . '/';
$file = substr($file, 2);
}

return $host . $file . '?' . $postfix;
return $host . $file . '?' . $this->cache->load($fsPath);
}

}
2 changes: 0 additions & 2 deletions tests/config/test.neon
Expand Up @@ -8,6 +8,4 @@ extensions:
assetsExtension: h4kuna\Assets\DI\AssetsExtension

assetsExtension:
version: '2017-04-17'
debugMode: FALSE
wwwDir: %appDir%
6 changes: 5 additions & 1 deletion tests/src/BasicRunTest.phpt
Expand Up @@ -5,9 +5,13 @@ use h4kuna\Assets,

$container = require __DIR__ . '/../bootsrap.php';

touch(__DIR__ . '/../config/test.neon', 536284800);

/* @var $file Fio\Nette\FioFactory */
$file = $container->getByType(Assets\File::class);

Assert::true($file instanceof Assets\File);

Assert::same('/config/test.neon?2017-04-17', $file->createUrl('config/test.neon'));
Assert::same('/config/test.neon?536284800', $file->createUrl('config/test.neon'));

$file->createUrl('config/php-unix.ini');

0 comments on commit c773c41

Please sign in to comment.