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: $builder = $this->getContainerBuilder();
31:
32: if (extension_loaded('pdo_sqlite')) {
33: $builder->addDefinition($this->prefix('journal'))
34: ->setClass(Nette\Caching\Storages\IJournal::class)
35: ->setFactory(Nette\Caching\Storages\SQLiteJournal::class, [$this->tempDir . '/cache/journal.s3db']);
36: }
37:
38: $builder->addDefinition($this->prefix('storage'))
39: ->setClass(Nette\Caching\IStorage::class)
40: ->setFactory(Nette\Caching\Storages\FileStorage::class, [$this->tempDir . '/cache']);
41:
42: if ($this->name === 'cache') {
43: if (extension_loaded('pdo_sqlite')) {
44: $builder->addAlias('nette.cacheJournal', $this->prefix('journal'));
45: }
46: $builder->addAlias('cacheStorage', $this->prefix('storage'));
47: }
48: }
49:
50:
51: public function afterCompile(Nette\PhpGenerator\ClassType $class)
52: {
53: if (!$this->checkTempDir($this->tempDir . '/cache')) {
54: $class->getMethod('initialize')->addBody('Nette\Caching\Storages\FileStorage::$useDirectories = false;');
55: }
56: }
57:
58:
59: private function checkTempDir($dir)
60: {
61: Nette\Utils\FileSystem::createDir($dir);
62:
63:
64: $uniq = uniqid('_', true);
65: if (!@mkdir("$dir/$uniq")) {
66: throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
67: }
68:
69:
70: $isWritable = @file_put_contents("$dir/$uniq/_", '') !== false;
71: if ($isWritable) {
72: unlink("$dir/$uniq/_");
73: }
74: rmdir("$dir/$uniq");
75: return $isWritable;
76: }
77: }
78: