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