Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add own cache for same behavior on production and development mode.
- Loading branch information
Milan Matějček
committed
Apr 18, 2017
1 parent
548f676
commit c773c41
Showing
7 changed files
with
90 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) . ';'); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters