Skip to content

Commit d349d3f

Browse files
committed
added ContainerLoader: humble replacement for ContainerFactory
1 parent 1864d6e commit d349d3f

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

src/DI/ContainerFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* DI container generator.
1515
*
1616
* @author David Grudl
17+
* @deprecated
1718
*/
1819
class ContainerFactory extends Nette\Object
1920
{
@@ -122,7 +123,7 @@ private function loadClass()
122123
flock($handle, LOCK_SH);
123124
foreach ((array) @unserialize(file_get_contents("$file.meta")) as $f => $time) { // @ - file may not exist
124125
if (@filemtime($f) !== $time) { // @ - stat may fail
125-
unlink($file);
126+
@unlink($file); // @ - file may not exist
126127
break;
127128
}
128129
}

src/DI/ContainerLoader.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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);
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\ContainerLoader basic usage.
5+
*/
6+
7+
use Nette\DI,
8+
Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
$cache = new DI\ContainerLoader(TEMP_DIR . '/subdir');
15+
16+
$key = array(1, 2);
17+
$className = $cache->getClassName($key);
18+
Assert::match('Container%[\w]+%', $className);
19+
20+
$container = $cache->load($key, function($class) {
21+
return array("class $class {}", array());
22+
});
23+
Assert::type($className, new $container);

0 commit comments

Comments
 (0)