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
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

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