[READY] CModule::setComponents() proper fix 2 #1491

Merged
merged 6 commits into from Sep 29, 2012

Conversation

Projects
None yet
2 participants
@creocoder
Contributor

creocoder commented Sep 27, 2012

For example my base admin controller without fix:

<?php
class ControllerAdmin extends CController
{
    ...
    public function init()
    {
        parent::init();
        Yii::app()->user->loginUrl=array('admin/login');
        Yii::app()->errorHandler->errorAction='admin/error';
    }
}

What we see here? The components will be loaded regardless of whether they will be used in the code. This is not proper way. We should not encourage this. The right way:

<?php
class ControllerAdmin extends CController
{
    ...
    public function init()
    {
        parent::init();

        Yii::app()->setComponents(array(
            'user'=>array(
                'loginUrl'=>array('admin/login'),
            ),
            'errorHandler'=>array(
                'errorAction'=>'admin/error',
            ),
        ));
    }
}

Programmer does not have to worry about the components are loaded or not. If they change config at runtime level they know what they do. They will not try change CHttpSession::autoStart property at runtime because this property has no effect after component initialization.

With this fix we have proper workflow. For example:

<?php
Yii::app()->getComponent('errorHandler'); //CErrorHandler here

Yii::app()->setComponents(array(
    'errorHandler'=>array(
            'class'=>'AnotherErrorHandlerClass',
    ),
));

Yii::app()->getComponent('errorHandler'); //AnotherErrorHandlerClass here
Yii::app()->setComponent('errorHandler',null);
Yii::app()->getComponent('errorHandler'); //AnotherErrorHandlerClass here!

We need this fix because this features very needed for advanced usage. For example Symfony Service Container have all this features and ALWAYS have optimal lazy loading component workflow. Also any loaded component could be reconfigured at runtime level. Our framework need this features, because this is core features, it's needed by programmers interested in a perfect app memory consumption.

@creocoder

This comment has been minimized.

Show comment Hide comment
@creocoder

creocoder Sep 27, 2012

Contributor

@qiangxue Can you review new slightly improved version?

Contributor

creocoder commented Sep 27, 2012

@qiangxue Can you review new slightly improved version?

@creocoder

This comment has been minimized.

Show comment Hide comment
@creocoder

creocoder Sep 29, 2012

Contributor

@qiangxue If all ok we can merge it?

Contributor

creocoder commented Sep 29, 2012

@qiangxue If all ok we can merge it?

qiangxue added a commit that referenced this pull request Sep 29, 2012

Merge pull request #1491 from creocoder/cmodule-set-components-enh-2
[READY] CModule::setComponents() proper fix 2

@qiangxue qiangxue merged commit e2fa220 into yiisoft:master Sep 29, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment