|
| 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\DI; |
| 9 | + |
| 10 | +use Nette; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * DI container loader. |
| 15 | + * |
| 16 | + * @author David Grudl |
| 17 | + */ |
| 18 | +class ContainerLoader extends Nette\Object |
| 19 | +{ |
| 20 | + /** @var bool */ |
| 21 | + private $autoRebuild = FALSE; |
| 22 | + |
| 23 | + /** @var string */ |
| 24 | + private $tempDirectory; |
| 25 | + |
| 26 | + |
| 27 | + public function __construct($tempDirectory, $autoRebuild = FALSE) |
| 28 | + { |
| 29 | + $this->tempDirectory = $tempDirectory; |
| 30 | + $this->autoRebuild = $autoRebuild; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + * @param mixed |
| 36 | + * @param callable function(string $class): [code, files] |
| 37 | + * @return string |
| 38 | + */ |
| 39 | + public function load($key, $generator) |
| 40 | + { |
| 41 | + $class = $this->getClassName($key); |
| 42 | + if (!class_exists($class)) { |
| 43 | + $this->loadFile($class, $generator); |
| 44 | + } |
| 45 | + return $class; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function getClassName($key) |
| 53 | + { |
| 54 | + return 'Container_' . substr(md5(serialize($key)), 0, 10); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + private function loadFile($class, $generator) |
| 62 | + { |
| 63 | + $file = "$this->tempDirectory/$class.php"; |
| 64 | + if (!$this->autoRebuild && (@include $file) !== FALSE) { // @ - file may not exist |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (!is_dir($this->tempDirectory)) { |
| 69 | + @mkdir($this->tempDirectory); // @ - directory may already exist |
| 70 | + } |
| 71 | + $handle = fopen("$file.tmp", 'c+'); |
| 72 | + if (!$handle) { |
| 73 | + throw new Nette\IOException("Unable to open or create file '$file.tmp'."); |
| 74 | + } |
| 75 | + |
| 76 | + if ($this->autoRebuild) { |
| 77 | + flock($handle, LOCK_SH); |
| 78 | + foreach ((array) @unserialize(file_get_contents("$file.meta")) as $f => $time) { // @ - file may not exist |
| 79 | + if (@filemtime($f) !== $time) { // @ - stat may fail |
| 80 | + @unlink($file); // @ - file may not exist |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!is_file($file)) { |
| 87 | + flock($handle, LOCK_EX); |
| 88 | + if (!is_file($file)) { |
| 89 | + list($code, $dependencies) = call_user_func($generator, $class); |
| 90 | + if (!file_put_contents($file, "<?php\n" . $code)) { |
| 91 | + throw new Nette\IOException("Unable to write file '$file'."); |
| 92 | + } |
| 93 | + $tmp = array(); |
| 94 | + foreach ((array) $dependencies as $f) { |
| 95 | + $tmp[$f] = @filemtime($f); // @ - stat may fail |
| 96 | + } |
| 97 | + file_put_contents("$file.meta", serialize($tmp)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + require $file; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments