craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.21k stars 624 forks source link

Plugin::getSettings() is called before the plugin is instantiated #1548

Closed khalwat closed 7 years ago

khalwat commented 7 years ago

craft\services\Plugins::createPlugin() ends up calling craft\base\Plugin::setSettings(), which does:

    public function setSettings(array $settings)
    {
        $this->getSettings()->setAttributes($settings, false);
    }

This normally would be fine, but since the plugin isn't instantiated yet, you can't do things like Craft::$app->config->get('somesetting', 'someplugin'); or Craft::t('someplugin', 'some translation'); from within the overridden getSettings() method, which ends up being fairly problematic, as there's no other bottleneck I'm aware of like normalSettings() or such that we can hook in to do this.

I my case, I'm attempting to combine the settings from the db with the settings in my plugins config.php ala https://github.com/craftcms/cms/issues/1096

Stack trace:

1. in /home/vagrant/webdev/craft/craft3-disqus/src/Disqus.php at line 78
2. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/base/Component.php at line 172 – craft\base\Plugin::setSettings()
3. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/BaseYii.php at line 525 – yii\base\Component::__set()
4. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/base/Object.php at line 105 – yii\BaseYii::configure()
5. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/base/Module.php at line 158 – yii\base\Object::__construct()
6. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/di/Container.php at line 381 – yii\base\Module::__construct()
7. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/di/Container.php at line 381 – ReflectionClass::newInstanceArgs()
8. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/di/Container.php at line 156 – yii\di\Container::build()
9. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/BaseYii.php at line 344 – yii\di\Container::get()
10. in /home/vagrant/sites/craftcms/cms/src/services/Plugins.php at line 713 – yii\BaseYii::createObject()
11. in /home/vagrant/sites/craftcms/cms/src/services/Plugins.php at line 201 – craft\services\Plugins::createPlugin()
12. in /home/vagrant/sites/craftcms/cms/src/base/ApplicationTrait.php at line 1233 – craft\services\Plugins::loadPlugins()
13. in /home/vagrant/sites/craftcms/cms/src/web/Application.php at line 83 – craft\web\Application::_init()
14. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/base/Object.php at line 107 – craft\web\Application::init()
15. in /home/vagrant/sites/craft3/vendor/yiisoft/yii2/base/Application.php at line 205 – yii\base\Object::__construct()
16. in /home/vagrant/sites/craftcms/cms/src/web/Application.php at line 71 – yii\base\Application::__construct()
17. in /home/vagrant/sites/craftcms/cms/bootstrap/bootstrap.php at line 215 – craft\web\Application::__construct()
18. in /home/vagrant/sites/craftcms/cms/bootstrap/web.php at line 61 – unknown()
19. in /home/vagrant/sites/craft3/public/index.php at line 16 – unknown()
brandonkelly commented 7 years ago

Should no longer be an issue after b76977ac9564ee7d93c7f7edec82a2cd647d5cd4. Now you can just call $this->getSettings() from your init() method, rather than go through the Config service.