Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • CacheExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (https://nette.org)
 5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
 6:  */
 7: 
 8: namespace Nette\Bridges\CacheDI;
 9: 
10: use Nette;
11: 
12: 
13: /**
14:  * Cache extension for Nette DI.
15:  */
16: class CacheExtension extends Nette\DI\CompilerExtension
17: {
18:     /** @var string */
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:         // checks whether directory is writable
64:         $uniq = uniqid('_', true);
65:         if (!@mkdir("$dir/$uniq")) { // @ - is escalated to exception
66:             throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
67:         }
68: 
69:         // checks whether subdirectory is writable
70:         $isWritable = @file_put_contents("$dir/$uniq/_", '') !== false; // @ - error is expected
71:         if ($isWritable) {
72:             unlink("$dir/$uniq/_");
73:         }
74:         rmdir("$dir/$uniq");
75:         return $isWritable;
76:     }
77: }
78: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0