aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/di/proxy_instantiator.php
blob: 1f51100493bcc9c29a6aea83cff091fcbefd09e7 (plain)
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
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\di;

use \bantu\IniGetWrapper\IniGetWrapper;
use ProxyManager\Configuration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;

/**
 * Runtime lazy loading proxy generator extended for allowing use while using
 * open_basedir restrictions
 *
 * Original author: Marco Pivetta <ocramius@gmail.com>
 */
class proxy_instantiator implements InstantiatorInterface
{
	/**
	 * @var LazyLoadingValueHolderFactory
	 */
	private $factory;

	/**
	 * proxy_instantiator constructor
	 * @param string $cache_dir Cache dir for fall back when using open_basedir
	 */
	public function __construct($cache_dir)
	{
		$config = new Configuration();

		// Prevent trying to write to system temp dir in case of open_basedir
		// restrictions being in effect
		$ini_wrapper = new IniGetWrapper();
		if ($ini_wrapper->getString('open_basedir') || !file_exists(sys_get_temp_dir()))
		{
			$config->setProxiesTargetDir($cache_dir);
		}
		$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());

		$this->factory = new LazyLoadingValueHolderFactory($config);
	}

	/**
	 * {@inheritdoc}
	 */
	public function instantiateProxy(ContainerInterface $container, Definition $definition, $id, $realInstantiator)
	{
		return $this->factory->createProxy(
			$definition->getClass(),
			function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($realInstantiator) {
				$wrappedInstance = call_user_func($realInstantiator);

				$proxy->setProxyInitializer(null);

				return true;
			}
		);
	}
}