From 1353841739f8dc788e1519814f1955cb5347160f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 11 May 2016 08:48:45 +0200 Subject: [PATCH] Compiler::compile() and generateCode() arguments are deprecated and compile() always returns string (BC break) --- src/DI/Compiler.php | 23 ++++++++++++++-------- tests/DI/Compiler.parameters.circular.phpt | 2 +- tests/DI/DIExtension.accessors.phpt | 2 +- tests/DI/DIExtension.run.phpt | 2 +- tests/bootstrap.php | 4 +++- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/DI/Compiler.php b/src/DI/Compiler.php index 873cf4358..68f526a0b 100644 --- a/src/DI/Compiler.php +++ b/src/DI/Compiler.php @@ -143,18 +143,20 @@ public function getDependencies() /** - * @return Nette\PhpGenerator\ClassType[]|string + * @return string */ public function compile(array $config = NULL, $className = NULL, $parentName = NULL) { - $this->config = $config ?: $this->config; + if (func_num_args()) { + trigger_error(__METHOD__ . ' arguments are deprecated, use Compiler::addConfig() and Compiler::setClassName().', E_USER_DEPRECATED); + $this->config = func_get_arg(0) ?: $this->config; + $this->className = @func_get_arg(1) ?: $this->className; + } $this->processParameters(); $this->processExtensions(); $this->processServices(); - $classes = $this->generateCode($className ?: $this->className, $parentName); - return func_num_args() - ? implode("\n\n\n", $classes) // back compatiblity - : $classes; + $classes = $this->generateCode(); + return implode("\n\n\n", $classes); } @@ -212,8 +214,13 @@ public function processServices() /** @internal */ - public function generateCode($className, $parentName = NULL) + public function generateCode() { + if (func_num_args()) { + trigger_error(__METHOD__ . ' arguments are deprecated, use Compiler::setClassName().', E_USER_DEPRECATED); + $this->className = func_get_arg(0) ?: $this->className; + } + $this->builder->prepareClassList(); foreach ($this->extensions as $extension) { @@ -221,7 +228,7 @@ public function generateCode($className, $parentName = NULL) $this->dependencies[] = (new \ReflectionClass($extension))->getFileName(); } - $classes = $this->builder->generateClasses($className, $parentName); + $classes = $this->builder->generateClasses($this->className); $classes[0]->addMethod('initialize'); $this->addDependencies($this->builder->getDependencies()); diff --git a/tests/DI/Compiler.parameters.circular.phpt b/tests/DI/Compiler.parameters.circular.phpt index b13b7b20f..f60de48d6 100644 --- a/tests/DI/Compiler.parameters.circular.phpt +++ b/tests/DI/Compiler.parameters.circular.phpt @@ -14,5 +14,5 @@ require __DIR__ . '/../bootstrap.php'; Assert::exception(function () { $loader = new DI\Config\Loader; $compiler = new DI\Compiler; - $compiler->compile($loader->load('files/compiler.parameters.circular.ini'), 'Container', Nette\DI\Container::class); + $compiler->addConfig($loader->load('files/compiler.parameters.circular.ini'))->compile(); }, Nette\InvalidArgumentException::class, 'Circular reference detected for variables: foo, foobar, bar.'); diff --git a/tests/DI/DIExtension.accessors.phpt b/tests/DI/DIExtension.accessors.phpt index e4048766e..8fef6e8c3 100644 --- a/tests/DI/DIExtension.accessors.phpt +++ b/tests/DI/DIExtension.accessors.phpt @@ -23,7 +23,7 @@ services: std: stdClass ', 'neon')); -eval($compiler->compile($config, 'Container1')); +eval($compiler->addConfig($config)->setClassName('Container1')->compile()); $container = new Container1; Assert::type(stdClass::class, $container->std); diff --git a/tests/DI/DIExtension.run.phpt b/tests/DI/DIExtension.run.phpt index 725216691..8f38f58f9 100644 --- a/tests/DI/DIExtension.run.phpt +++ b/tests/DI/DIExtension.run.phpt @@ -20,7 +20,7 @@ services: std: {class: stdClass, tags: [run]} ', 'neon')); -eval($compiler->compile($config, 'Container1')); +eval($compiler->addConfig($config)->setClassName('Container1')->compile()); $container = new Container1; Assert::false($container->isCreated('std')); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c6600c5b3..ee0ff4fe0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -59,7 +59,9 @@ function createContainer($source, $config = NULL) $loader = new Nette\DI\Config\Loader; $config = $loader->load(is_file($config) ? $config : Tester\FileMock::create($config, 'neon')); } - $code = $source->compile((array) $config, $class, Nette\DI\Container::class); + $code = $source->addConfig((array) $config) + ->setClassName($class) + ->compile(); } else { return; }