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

  • Compiler
  • CompilerExtension
  • Container
  • ContainerBuilder
  • ContainerFactory
  • ContainerLoader
  • ServiceDefinition
  • Statement

Exceptions

  • MissingServiceException
  • ServiceCreationException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  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\DI;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * DI container loader.
 15:  */
 16: class ContainerLoader extends Nette\Object
 17: {
 18:     /** @var bool */
 19:     private $autoRebuild = FALSE;
 20: 
 21:     /** @var string */
 22:     private $tempDirectory;
 23: 
 24: 
 25:     public function __construct($tempDirectory, $autoRebuild = FALSE)
 26:     {
 27:         $this->tempDirectory = $tempDirectory;
 28:         $this->autoRebuild = $autoRebuild;
 29:     }
 30: 
 31: 
 32:     /**
 33:      * @param  mixed
 34:      * @param  callable  function (Nette\DI\Compiler $compiler): string|NULL
 35:      * @return string
 36:      */
 37:     public function load($key, $generator)
 38:     {
 39:         $class = $this->getClassName($key);
 40:         if (!class_exists($class, FALSE)) {
 41:             $this->loadFile($class, $generator);
 42:         }
 43:         return $class;
 44:     }
 45: 
 46: 
 47:     /**
 48:      * @return string
 49:      */
 50:     public function getClassName($key)
 51:     {
 52:         return 'Container_' . substr(md5(serialize($key)), 0, 10);
 53:     }
 54: 
 55: 
 56:     /**
 57:      * @return void
 58:      */
 59:     private function loadFile($class, $generator)
 60:     {
 61:         $file = "$this->tempDirectory/$class.php";
 62:         if (!$this->isExpired($file) && (@include $file) !== FALSE) { // @ file may not exist
 63:             return;
 64:         }
 65: 
 66:         if (!is_dir($this->tempDirectory)) {
 67:             @mkdir($this->tempDirectory); // @ - directory may already exist
 68:         }
 69: 
 70:         $handle = fopen("$file.lock", 'c+');
 71:         if (!$handle || !flock($handle, LOCK_EX)) {
 72:             throw new Nette\IOException("Unable to acquire exclusive lock on '$file.lock'.");
 73:         }
 74: 
 75:         if (!is_file($file) || $this->isExpired($file)) {
 76:             list($toWrite[$file], $toWrite["$file.meta"]) = $this->generate($class, $generator);
 77: 
 78:             foreach ($toWrite as $name => $content) {
 79:                 if (file_put_contents("$name.tmp", $content) !== strlen($content) || !rename("$name.tmp", $name)) {
 80:                     @unlink("$name.tmp"); // @ - file may not exist
 81:                     throw new Nette\IOException("Unable to create file '$name'.");
 82:                 }
 83:             }
 84:         }
 85: 
 86:         if ((@include $file) === FALSE) { // @ - error escalated to exception
 87:             throw new Nette\IOException("Unable to include '$file'.");
 88:         }
 89:         flock($handle, LOCK_UN);
 90:     }
 91: 
 92: 
 93:     private function isExpired($file)
 94:     {
 95:         if ($this->autoRebuild) {
 96:             $meta = @unserialize(file_get_contents("$file.meta")); // @ - file may not exist
 97:             $files = $meta ? array_combine($tmp = array_keys($meta), $tmp) : array();
 98:             return $meta !== @array_map('filemtime', $files); // @ - files may not exist
 99:         }
100:         return FALSE;
101:     }
102: 
103: 
104:     /**
105:      * @return array of (code, file[])
106:      */
107:     protected function generate($class, $generator)
108:     {
109:         $compiler = new Compiler;
110:         $compiler->getContainerBuilder()->setClassName($class);
111:         $code = call_user_func_array($generator, array(& $compiler));
112:         $code = $code ?: implode("\n\n\n", $compiler->compile());
113:         $files = $compiler->getDependencies();
114:         $files = $files ? array_combine($files, $files) : array(); // workaround for PHP 5.3 array_combine
115:         return array(
116:             "<?php\n$code",
117:             serialize(@array_map('filemtime', $files)), // @ - file may not exist
118:         );
119:     }
120: 
121: }
122: 
Nette 2.3.8 API API documentation generated by ApiGen 2.8.0