Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI container #2788

Merged
merged 12 commits into from Mar 21, 2014
Merged

DI container #2788

merged 12 commits into from Mar 21, 2014

Conversation

@qiangxue
Copy link
Member

qiangxue commented Mar 18, 2014

Fixes #503

For performance reason, automatic constructor injection is not implemented.

The most obvious BC breaking change to existing code is the renaming from Application::getComponent() to get().

  • refactor existing app components to make use of Instance
  • controller creation should make use of container
  • guide titled "Dependency injection and service locator"
*/
public static function of($id, ContainerInterface $container = null)
{
return new self($id, $container);

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

maybe new static ?

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

We do not expect Instance to be subclassed since it's usage is mainly via static method calls.

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

well, but better to leave this ability to user as for me . Because Html helper in almost all cases will not be overrided but it has static .

*/
public function set($typeOrID, $definition)
{
if ($notShared = $typeOrID[0] === '*') {

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

can we mark not-shared as ! ? It will be more intuitive .

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

* means "many", while ! can be interpreted differently, depending on the context (it could mean important or "not").

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

yes, it means not - not shared.

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

Well, it depends on what word is used after not. It may cause confusion. Also !db reads more like not db if you want to interpret it as "not".

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

maybe ~ ? because * almost always means all and it confusing, while ~ can mean different constraints .

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

Only when * is used alone, it means all. When it is used with other things, it means repeating.
~ is almost reserved to represent "home directory".

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

but as you said it means repeating, and repeating many times like multiply command . But we actually dont repeat anything we want to not allow to repeat .

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

notShared = a new instance will be created each time get() is called. So it does mean repeating.

This comment has been minimized.

Copy link
@Ragazzo

Ragazzo Mar 18, 2014

Contributor

ok

@Ragazzo
Copy link
Contributor

Ragazzo commented Mar 18, 2014

Do we really need Instance ? since Container already creates components if needed .

@samdark
Copy link
Member

samdark commented Mar 18, 2014

https://github.com/container-interop/container-interop created by many framework representatives. At least Zend Framework, Laravel, Aura, Joomla Framework, Acclimate, Molajo, Mouf, PHP-DI participated. Worth checking.

@Ragazzo
Copy link
Contributor

Ragazzo commented Mar 18, 2014

currently there is nothing in that repo except several files, where is all discussion or what need to be checked ?

@samdark
Copy link
Member

samdark commented Mar 18, 2014

Interface and meta. It seems we already have get and has btw.

@Ragazzo
Copy link
Contributor

Ragazzo commented Mar 18, 2014

i see, not a big discussion overall . Maybe we need to add has method .

@samdark
Copy link
Member

samdark commented Mar 18, 2014

We have it.

@Ragazzo
Copy link
Contributor

Ragazzo commented Mar 18, 2014

right ) maybe then ArrayAccess ? Could be useful )

* use yii\di\Container;
* use yii\di\Instance;
*
* interface UserFinderInterface

This comment has been minimized.

Copy link
@samdark

samdark Mar 18, 2014

Member

Need to mention that it has to be a separate file.

@samdark
Copy link
Member

samdark commented Mar 18, 2014

Overall looks good to me. It should be unit-tested properly since it's the very core thing.

@@ -130,15 +131,14 @@ public function __construct($id, $parent = null, $config = [])

/**
* Getter magic method.
* This method is overridden to support accessing components
* like reading module properties.
* This method is overridden to support accessing components like reading properties.
* @param string $name component or property name
* @return mixed the named property value
*/
public function __get($name)

This comment has been minimized.

Copy link
@cebe

cebe Mar 18, 2014

Member

shouldn't this be part of the trait?

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

Not quite sure as it assumes the existence of parent::__get().

*
* interface UserFinderInterface
* {
* function findUser();

This comment has been minimized.

Copy link
@cebe

cebe Mar 18, 2014

Member

should be public function.

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 18, 2014

Author Member

All interface methods must be public.

* function findUser();
* }
*
* class UserFinder extends Object implements UserFinderInterface

This comment has been minimized.

Copy link
@cebe

cebe Mar 18, 2014

Member

also shouldn't classes that are called components in this example extend from Component for better understanding? I know it does not make a difference from functional point of view.

@@ -35,7 +35,7 @@ public function init()
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
$this->db = Yii::$app->get($this->db);

This comment has been minimized.

Copy link
@cebe

cebe Mar 18, 2014

Member

shouldn't this code be rewritten to use Instance::ensure?

@nkt

This comment has been minimized.

Copy link

nkt commented on framework/di/ContainerTrait.php in 66abd5b Mar 19, 2014

Why not just is_callable?

qiangxue added a commit that referenced this pull request Mar 21, 2014
DI container
@qiangxue qiangxue merged commit 3d0a266 into master Mar 21, 2014
@qiangxue qiangxue deleted the feature-container branch Mar 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

5 participants
You can’t perform that action at this time.