1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\CacheDI;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class CacheExtension extends Nette\DI\CompilerExtension
17: {
18:
19: private $tempDir;
20:
21:
22: public function __construct($tempDir)
23: {
24: $this->tempDir = $tempDir;
25: }
26:
27:
28: public function loadConfiguration()
29: {
30: $container = $this->getContainerBuilder();
31:
32: $container->addDefinition($this->prefix('journal'))
33: ->setClass('Nette\Caching\Storages\IJournal')
34: ->setFactory('Nette\Caching\Storages\FileJournal', array($this->tempDir));
35:
36: $container->addDefinition($this->prefix('storage'))
37: ->setClass('Nette\Caching\IStorage')
38: ->setFactory('Nette\Caching\Storages\FileStorage', array($this->tempDir . '/cache'));
39:
40: if ($this->name === 'cache') {
41: $container->addAlias('nette.cacheJournal', $this->prefix('journal'));
42: $container->addAlias('cacheStorage', $this->prefix('storage'));
43: }
44: }
45:
46:
47: public function afterCompile(Nette\PhpGenerator\ClassType $class)
48: {
49: if (!$this->checkTempDir($this->tempDir . '/cache')) {
50: $class->getMethod('initialize')->addBody('Nette\Caching\Storages\FileStorage::$useDirectories = FALSE;');
51: }
52: }
53:
54:
55: private function checkTempDir($dir)
56: {
57: @mkdir($dir);
58:
59:
60: $uniq = uniqid('_', TRUE);
61: if (!@mkdir("$dir/$uniq")) {
62: throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
63: }
64:
65:
66: $isWritable = @file_put_contents("$dir/$uniq/_", '') !== FALSE;
67: if ($isWritable) {
68: unlink("$dir/$uniq/_");
69: }
70: rmdir("$dir/$uniq");
71: return $isWritable;
72: }
73:
74: }
75: