Skip to content

Commit

Permalink
#235 Replaced link() with get() and marked link() as deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Mar 21, 2015
1 parent b5f9952 commit b6067e6
Show file tree
Hide file tree
Showing 35 changed files with 137 additions and 109 deletions.
1 change: 1 addition & 0 deletions change-log.md
Expand Up @@ -19,6 +19,7 @@ Improvements:
return new Foo();
}
```
- [#235](https://github.com/mnapoli/PHP-DI/issues/235) `DI\link()` is now deprecated in favor of `DI\get()`. There is no BC break as `DI\link()` still works.

BC breaks:

Expand Down
2 changes: 1 addition & 1 deletion doc/best-practices.md
Expand Up @@ -126,7 +126,7 @@ Example:
return [
// ...
OrderService::class => DI\object()
->constructor(DI\link(SomeOtherService::class), 'a value'),
->constructor(DI\get(SomeOtherService::class), 'a value'),
];
```

Expand Down
2 changes: 1 addition & 1 deletion doc/container.md
Expand Up @@ -122,7 +122,7 @@ Note that you can also define injections on the fly if you don't use type-hints:

```php
$parameters = [
'logger' => \DI\link('Logger')
'logger' => \DI\get('Logger')
];

$container->call(function ($logger) {
Expand Down
4 changes: 2 additions & 2 deletions doc/definition-overriding.md
Expand Up @@ -49,7 +49,7 @@ You can go even further by overriding this definition using file-based definitio

return [
'Foo' => DI\object()
->constructor(DI\link('another.specific.service')),
->constructor(DI\get('another.specific.service')),

'another.specific.service' => DI\object('Bar'),
];
Expand All @@ -59,5 +59,5 @@ Finally, you can also override the file-based definition by directly calling the

```php
$container->set('Foo')
->constructor(DI\link('yet.another.specific.service'));
->constructor(DI\get('yet.another.specific.service'));
```
18 changes: 9 additions & 9 deletions doc/definition.md
Expand Up @@ -164,7 +164,7 @@ PHP-DI provides function helpers for this (to define *values*, you don't need a

- `DI\object($classname = null)`: define an object entry
- `DI\factory($factory)`: define a factory that returns an entry
- `DI\link($entryName)`: used to define alias entries, and also to reference other entries in object definitions (see below)
- `DI\get($entryName)`: used to define alias entries, and also to reference other entries in object definitions (see below) - was previously `DI\link()` in PHP-DI 4, which is kept for backward compatibility
- `DI\value($value)`: defines a simple value. This helper is not needed as anything is a value by default. The only use case for this helper is to define a container entry that is a closure (as closure are turned into factory definitions automatically)

Example of a `config.php` file (using [PHP 5.4 short arrays](http://php.net/manual/en/migration54.new-features.php)):
Expand All @@ -191,18 +191,18 @@ return [

// Defines an instance of My\Class
'My\Class' => DI\object()
->constructor(DI\link('db.host'), DI\link('My\OtherClass')),
->constructor(DI\get('db.host'), DI\get('My\OtherClass')),

'My\OtherClass' => DI\object()
->scope(Scope::PROTOTYPE())
->constructor(DI\link('db.host'), DI\link('db.port'))
->method('setFoo2', DI\link('My\Foo1'), DI\link('My\Foo2'))
->constructor(DI\get('db.host'), DI\get('db.port'))
->method('setFoo2', DI\get('My\Foo1'), DI\get('My\Foo2'))
->property('bar', 'My\Bar'),

// Define only specific parameters
'My\AnotherClass' => DI\object()
->constructorParameter('someParam', 'value to inject')
->methodParameter('setFoo2', 'someParam', DI\link('My\Foo')),
->methodParameter('setFoo2', 'someParam', DI\get('My\Foo')),

// Mapping an interface to an implementation
'My\Interface' => DI\object('My\Implementation'),
Expand All @@ -222,19 +222,19 @@ return [
})->scope(Scope::PROTOTYPE()),

// Defining an alias to another entry
'some.entry' => DI\link('some.other.entry'),
'some.entry' => DI\get('some.other.entry'),

// Defining a value based on an environment variable
'db1.url' => DI\env('DATABASE_URL'),
// With a default value
'db2.url' => DI\env('DATABASE_URL', 'postgresql://user:pass@localhost/db'),
// With a default value that is another entry
'db2.host' => DI\env('DATABASE_HOST', DI\link('db.host')),
'db2.host' => DI\env('DATABASE_HOST', DI\get('db.host')),

// Arrays can contain links to other entries
'log.handlers' => [
DI\link('Monolog\Handler\StreamHandler'),
DI\link('Monolog\Handler\EmailHandler'),
DI\get('Monolog\Handler\StreamHandler'),
DI\get('Monolog\Handler\EmailHandler'),
],

];
Expand Down
4 changes: 2 additions & 2 deletions doc/environments.md
Expand Up @@ -14,7 +14,7 @@ return [
'db.port' => 3336,

'DbAdapter' => DI\object()
->constructor(DI\link('db.host'), DI\link('db.port')),
->constructor(DI\get('db.host'), DI\get('db.port')),
];
```

Expand Down Expand Up @@ -43,7 +43,7 @@ return [
// config.php
return [
'DbAdapter' => DI\object()
->constructor(DI\link('db.host'), DI\link('db.port')),
->constructor(DI\get('db.host'), DI\get('db.port')),
];
```

Expand Down
6 changes: 3 additions & 3 deletions doc/frameworks/symfony2.md
Expand Up @@ -120,7 +120,7 @@ That's because PHP-DI is designed to play nice with others:
```php
return [
'Acme\MyBundle\Controller\ProductController' => DI\object()
->constructor(DI\link('doctrine.orm.entity_manager')),
->constructor(DI\get('doctrine.orm.entity_manager')),
];
```

Expand All @@ -139,9 +139,9 @@ like these:

```php
return [
'Psr\Log\LoggerInterface' => DI\link('logger'),
'Psr\Log\LoggerInterface' => DI\get('logger'),
// PHP 5.5 notation:
ObjectManager::class => DI\link('doctrine.orm.entity_manager'),
ObjectManager::class => DI\get('doctrine.orm.entity_manager'),
];
```

Expand Down
2 changes: 1 addition & 1 deletion doc/getting-started.md
Expand Up @@ -112,7 +112,7 @@ return [

// Class
MyDbAdapter::class => DI\object()
->constructor(DI\link('db.host'), DI\link('db.port')),
->constructor(DI\get('db.host'), DI\get('db.port')),

];
```
Expand Down
4 changes: 2 additions & 2 deletions src/DI/Definition/Dumper/AliasDefinitionDumper.php
Expand Up @@ -34,14 +34,14 @@ public function dump(Definition $definition)

if ($definition->getName()) {
return sprintf(
"link(%s => %s)",
"get(%s => %s)",
$definition->getName(),
$definition->getTargetEntryName()
);
}

return sprintf(
"link(%s)",
"get(%s)",
$definition->getTargetEntryName()
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DI/Definition/Dumper/ClassDefinitionDumper.php
Expand Up @@ -90,7 +90,7 @@ private function dumpProperties(ClassDefinition $definition)
foreach ($definition->getPropertyInjections() as $propertyInjection) {
$value = $propertyInjection->getValue();
if ($value instanceof EntryReference) {
$valueStr = sprintf('link(%s)', $value->getName());
$valueStr = sprintf('get(%s)', $value->getName());
} else {
$valueStr = var_export($value, true);
}
Expand Down Expand Up @@ -125,7 +125,7 @@ private function dumpMethodParameters($className, MethodInjection $methodInjecti
$value = $methodInjection->getParameter($index);

if ($value instanceof EntryReference) {
$args[] = sprintf('$%s = link(%s)', $parameter->getName(), $value->getName());
$args[] = sprintf('$%s = get(%s)', $parameter->getName(), $value->getName());
} else {
$args[] = sprintf('$%s = %s', $parameter->getName(), var_export($value, true));
}
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Definition/Dumper/FunctionCallDefinitionDumper.php
Expand Up @@ -55,7 +55,7 @@ private function dumpMethodParameters(
$value = $definition->getParameter($index);

if ($value instanceof EntryReference) {
$args[] = sprintf('$%s = link(%s)', $parameter->getName(), $value->getName());
$args[] = sprintf('$%s = get(%s)', $parameter->getName(), $value->getName());
} else {
$args[] = sprintf('$%s = %s', $parameter->getName(), var_export($value, true));
}
Expand Down
20 changes: 18 additions & 2 deletions src/DI/functions.php
Expand Up @@ -61,10 +61,26 @@ function factory($factory)
}
}

if (! function_exists('DI\get')) {
/**
* Helper for referencing another container entry in an object definition.
*
* @param string $entryName
*
* @return EntryReference
*/
function get($entryName)
{
return new EntryReference($entryName);
}
}

if (! function_exists('DI\link')) {
/**
* Helper for referencing another container entry in an object definition.
*
* @deprecated \DI\link() has been replaced by \DI\get()
*
* @param string $entryName
*
* @return EntryReference
Expand Down Expand Up @@ -99,12 +115,12 @@ function env($variableName, $defaultValue = null)
*
* Example:
*
* 'log.backends' => DI\add(DI\link('My\Custom\LogBackend'))
* 'log.backends' => DI\add(DI\get('My\Custom\LogBackend'))
*
* or:
*
* 'log.backends' => DI\add([
* DI\link('My\Custom\LogBackend')
* DI\get('My\Custom\LogBackend')
* ])
*
* @param mixed|array $values A value or an array of values to add to the array.
Expand Down
2 changes: 1 addition & 1 deletion tests/IntegrationTest/CallFunctionTest.php
Expand Up @@ -46,7 +46,7 @@ public function testParameterWithLink()
$result = $container->call(function($foo) {
return $foo;
}, array(
'foo' => \DI\link('bar'),
'foo' => \DI\get('bar'),
));
$this->assertEquals('bam', $result);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/IntegrationTest/Definitions/ArrayDefinitionTest.php
Expand Up @@ -41,8 +41,8 @@ public function test_array_with_links()
$builder = new ContainerBuilder();
$builder->addDefinitions(array(
'links' => array(
\DI\link('singleton'),
\DI\link('prototype'),
\DI\get('singleton'),
\DI\get('prototype'),
),
'singleton' => \DI\object('stdClass'),
'prototype' => \DI\object('stdClass')
Expand Down Expand Up @@ -87,7 +87,7 @@ public function test_array_with_prototype_entries()
$builder = new ContainerBuilder();
$builder->addDefinitions(array(
'array' => array(
\DI\link('prototype'),
\DI\get('prototype'),
),
'prototype' => \DI\object('stdClass')
->scope(Scope::PROTOTYPE()),
Expand All @@ -112,7 +112,7 @@ public function test_add_entries()
$builder->addDefinitions(array(
'values' => \DI\add(array(
'another value',
\DI\link('foo'),
\DI\get('foo'),
)),
'foo' => \DI\object('stdClass'),
));
Expand Down
Expand Up @@ -77,7 +77,7 @@ public function test_nonexistent_env_variable_with_other_entry_as_default()
{
$builder = new ContainerBuilder();
$builder->addDefinitions(array(
'var' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\link('foo')),
'var' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\get('foo')),
'foo' => 'bar',
));
$container = $builder->build();
Expand Down
8 changes: 4 additions & 4 deletions tests/IntegrationTest/Definitions/NestedDefinitionsTest.php
Expand Up @@ -28,7 +28,7 @@ public function should_allow_nested_definitions_in_environment_variables()

$builder->addDefinitions(array(
'foo' => 'bar',
'link' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\link('foo')),
'link' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\get('foo')),
'object' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\object('stdClass')),
));

Expand Down Expand Up @@ -59,7 +59,7 @@ public function should_allow_nested_definitions_in_object_definitions()
return $impl;
})
)
->property('property1', \DI\link('foo'))
->property('property1', \DI\get('foo'))
->property('property2', \DI\factory(function () use ($impl) {
return $impl;
})),
Expand Down Expand Up @@ -89,8 +89,8 @@ public function should_allow_nested_definitions_in_arrays()
$builder->addDefinitions(array(
'foo' => 'bar',
'array' => array(
'env' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\link('foo')),
'link' => \DI\link('foo'),
'env' => \DI\env('PHP_DI_DO_NOT_DEFINE_THIS', \DI\get('foo')),
'link' => \DI\get('foo'),
'object' => \DI\object('stdClass'),
),
));
Expand Down
30 changes: 15 additions & 15 deletions tests/IntegrationTest/Fixtures/definitions.php
Expand Up @@ -7,22 +7,22 @@

'DI\Test\IntegrationTest\Fixtures\Class1' => DI\object()
->scope(Scope::PROTOTYPE())
->property('property1', DI\link('DI\Test\IntegrationTest\Fixtures\Class2'))
->property('property2', DI\link('DI\Test\IntegrationTest\Fixtures\Interface1'))
->property('property3', DI\link('namedDependency'))
->property('property4', DI\link('foo'))
->property('property5', DI\link('DI\Test\IntegrationTest\Fixtures\LazyDependency'))
->property('property1', DI\get('DI\Test\IntegrationTest\Fixtures\Class2'))
->property('property2', DI\get('DI\Test\IntegrationTest\Fixtures\Interface1'))
->property('property3', DI\get('namedDependency'))
->property('property4', DI\get('foo'))
->property('property5', DI\get('DI\Test\IntegrationTest\Fixtures\LazyDependency'))
->constructor(
DI\link('DI\Test\IntegrationTest\Fixtures\Class2'),
DI\link('DI\Test\IntegrationTest\Fixtures\Interface1'),
DI\link('DI\Test\IntegrationTest\Fixtures\LazyDependency')
DI\get('DI\Test\IntegrationTest\Fixtures\Class2'),
DI\get('DI\Test\IntegrationTest\Fixtures\Interface1'),
DI\get('DI\Test\IntegrationTest\Fixtures\LazyDependency')
)
->method('method1', DI\link('DI\Test\IntegrationTest\Fixtures\Class2'))
->method('method2', DI\link('DI\Test\IntegrationTest\Fixtures\Interface1'))
->method('method3', DI\link('namedDependency'), DI\link('foo'))
->method('method4', DI\link('DI\Test\IntegrationTest\Fixtures\LazyDependency'))
->methodParameter('method5', 'param1', \DI\link('DI\Test\IntegrationTest\Fixtures\Interface1'))
->methodParameter('method5', 'param2', \DI\link('foo')),
->method('method1', DI\get('DI\Test\IntegrationTest\Fixtures\Class2'))
->method('method2', DI\get('DI\Test\IntegrationTest\Fixtures\Interface1'))
->method('method3', DI\get('namedDependency'), DI\get('foo'))
->method('method4', DI\get('DI\Test\IntegrationTest\Fixtures\LazyDependency'))
->methodParameter('method5', 'param1', \DI\get('DI\Test\IntegrationTest\Fixtures\Interface1'))
->methodParameter('method5', 'param2', \DI\get('foo')),

'DI\Test\IntegrationTest\Fixtures\Class2' => DI\object(),

Expand All @@ -37,5 +37,5 @@
'DI\Test\IntegrationTest\Fixtures\LazyDependency' => DI\object()
->lazy(),

'alias' => DI\link('namedDependency'),
'alias' => DI\get('namedDependency'),
);
16 changes: 8 additions & 8 deletions tests/IntegrationTest/InheritanceTest.php
Expand Up @@ -80,18 +80,18 @@ public static function containerProvider()
$containerPHPDefinitions->set(
'DI\Test\IntegrationTest\Fixtures\InheritanceTest\BaseClass',
\DI\object('DI\Test\IntegrationTest\Fixtures\InheritanceTest\SubClass')
->property('property1', \DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->property('property4', \DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->constructor(\DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->method('setProperty2', \DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->property('property1', \DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->property('property4', \DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->constructor(\DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->method('setProperty2', \DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
);
$containerPHPDefinitions->set(
'DI\Test\IntegrationTest\Fixtures\InheritanceTest\SubClass',
\DI\object()
->property('property1', \DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->property('property4', \DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->constructor(\DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->method('setProperty2', \DI\link('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->property('property1', \DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->property('property4', \DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->constructor(\DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
->method('setProperty2', \DI\get('DI\Test\IntegrationTest\Fixtures\InheritanceTest\Dependency'))
);

return array(
Expand Down

0 comments on commit b6067e6

Please sign in to comment.