/usr/share/php/Nette/DI/ContainerLoader.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\DI;
use Nette;
/**
* DI container loader.
*/
class ContainerLoader
{
use Nette\SmartObject;
/** @var bool */
private $autoRebuild = FALSE;
/** @var string */
private $tempDirectory;
public function __construct($tempDirectory, $autoRebuild = FALSE)
{
$this->tempDirectory = $tempDirectory;
$this->autoRebuild = $autoRebuild;
}
/**
* @param callable function (Nette\DI\Compiler $compiler): string|NULL
* @param mixed
* @return string
*/
public function load($generator, $key = NULL)
{
if (!is_callable($generator)) { // back compatiblity
trigger_error(__METHOD__ . ': order of arguments has been swapped.', E_USER_DEPRECATED);
list($generator, $key) = [$key, $generator];
}
$class = $this->getClassName($key);
if (!class_exists($class, FALSE)) {
$this->loadFile($class, $generator);
}
return $class;
}
/**
* @return string
*/
public function getClassName($key)
{
return 'Container_' . substr(md5(serialize($key)), 0, 10);
}
/**
* @return void
*/
private function loadFile($class, $generator)
{
$file = "$this->tempDirectory/$class.php";
if (!$this->isExpired($file) && (@include $file) !== FALSE) { // @ file may not exist
return;
}
if (!is_dir($this->tempDirectory)) {
@mkdir($this->tempDirectory); // @ - directory may already exist
}
$handle = fopen("$file.lock", 'c+');
if (!$handle || !flock($handle, LOCK_EX)) {
throw new Nette\IOException("Unable to acquire exclusive lock on '$file.lock'.");
}
if (!is_file($file) || $this->isExpired($file)) {
list($toWrite[$file], $toWrite["$file.meta"]) = $this->generate($class, $generator);
foreach ($toWrite as $name => $content) {
if (file_put_contents("$name.tmp", $content) !== strlen($content) || !rename("$name.tmp", $name)) {
@unlink("$name.tmp"); // @ - file may not exist
throw new Nette\IOException("Unable to create file '$name'.");
} elseif (function_exists('opcache_invalidate')) {
@opcache_invalidate($name, TRUE); // @ can be restricted
}
}
}
if ((@include $file) === FALSE) { // @ - error escalated to exception
throw new Nette\IOException("Unable to include '$file'.");
}
flock($handle, LOCK_UN);
}
private function isExpired($file)
{
if ($this->autoRebuild) {
$meta = @unserialize(file_get_contents("$file.meta")); // @ - file may not exist
return empty($meta[0]) || DependencyChecker::isExpired(...$meta);
}
return FALSE;
}
/**
* @return array of (code, file[])
*/
protected function generate($class, $generator)
{
$compiler = new Compiler;
$compiler->setClassName($class);
$code = call_user_func_array($generator, [& $compiler]) ?: $compiler->compile();
return [
"<?php\n$code",
serialize($compiler->exportDependencies())
];
}
}
|