Skip to content

Commit

Permalink
Compiler::compile() and generateCode() arguments are deprecated and c…
Browse files Browse the repository at this point in the history
…ompile() always returns string (BC break)
  • Loading branch information
dg committed May 11, 2016
1 parent d0a8856 commit 1353841
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
23 changes: 15 additions & 8 deletions src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -212,16 +214,21 @@ 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) {
$extension->beforeCompile();
$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());

Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.parameters.circular.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
2 changes: 1 addition & 1 deletion tests/DI/DIExtension.accessors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/DIExtension.run.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
4 changes: 3 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 1353841

Please sign in to comment.