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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • CacheExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
 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:         $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); // @ - directory may exists
58: 
59:         // checks whether directory is writable
60:         $uniq = uniqid('_', TRUE);
61:         if (!@mkdir("$dir/$uniq")) { // @ - is escalated to exception
62:             throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
63:         }
64: 
65:         // checks whether subdirectory is writable
66:         $isWritable = @file_put_contents("$dir/$uniq/_", '') !== FALSE; // @ - error is expected
67:         if ($isWritable) {
68:             unlink("$dir/$uniq/_");
69:         }
70:         rmdir("$dir/$uniq");
71:         return $isWritable;
72:     }
73: 
74: }
75: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0