Getting a Reference to a Zend_Application's Config Object - php

Is there a helper method/object/methodology for getting a reference to a Zend_Application's config resource?
I know I can do something like
$config = new Zend_Config_Ini($file, $environment);
but that's going to reload/parse the config file. I'm looking for a way to peek at the given configuration value for a running Zend_Application.
The larger issue I'm trying to solve is I want a Zend_Queue to use the same database settings as my default database resource. If there's a more "Zend Like" way of achieving this other than "get reference to config, read resource values" please feel free to share that as well!

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function run()
{
// make the config available to everyone
$config = $this->getOptions();
Zend_Registry::set('config', new Zend_Config($config));
parent::run();
}
}
Zend_Queue
there is code in Zend_Queue_Adapter_Db __construct if (isset($this->_options['dbAdapter'])) so u can do thomething like this
new Zend_Queue_Adapter_Db(array('dbAdapter' => Zend_Db_Table::getDefaultAdapter()));
because standart Zend_Application_Resource_Db can use config option resources.db.isDefaultTableAdapter = true
or u can put db adapter in registry and get it from there at any place

Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions()

Related

Location of config in an OpenCart extension

I am currently developing OpenCart extensions. I am looking through existing extensions to understand the architecture (and familiarizing MVC/PHPOO concepts).
Frequently, this line of code comes up:
$seller_email = $this->config->get('service_seller_email');
My interpretation based on reading this thread:
I understand that this is a variable assignment, where it's accessing a model (?) called config to retrieve a string called service_seller_email from the admin settings portal. However, I have tried navigating through the various MVC folders within OpenCart, and I could not find a specific config.php. Could you please correct my interpretation if it's wrong?
EDIT: To add to the confusion, the article I linked specifies that you need to load a config in a controller before using it, like $this->language->load('product/search');, but I cannot find a line such as $this->config->load('...'); or the like.
Your interpretation is incorrect. The config class is not considered as model (despite it is accessing DB directly) but as a system library class - that's why you can find it under system/library/ folder.
OpenCart has it's implementation of service locator (registry) which is used to to store the config object so that you can access it directly from anywhere (inside of OpenCart of course).
It is loaded in both index.php files (in root and under admin/) like this:
$config = new Config();
$registry->set('config', $config);
This $registry is our service locator and it is passed over to any controller in it's __construct() method. Each controller has also a magic method __get() which is responsible for retrieving objects from this $registry - take a look at abstract Controller class at system/engine/controller.php which each controller extends (and should extend).
That's why you can freely call directly
$this->config->get('some_config_key');
The first part, $this->config will fall into the __get() method passing over the 'config' value as a $key which in turn is passed over to next call
return $this->registry->get($key);
And since the config object is registered under 'config' key in our service locator, it is retrieved and returned so that we could call get('some_config_key') on it.
Now the some_config_key key is stored in DB in setting table and the config object tries to find it and get it's value (you can take a look at how this works in system/library/config.php).
Hopefully this clarifies your confusion.
Looking at the docs, it looks like the config values are stored in the setting table. The Config class itself is located in /system/library/config.php.
In admin > index.php you wil find
// Config
$config = new Config();
$registry->set('config', $config);
That's where it is initialized. The class is located in
system > library > config.php
Config loaded in system > engine >loader.php:
public function config($config) {
$this->config->load($config);
}

Yii database connection

Is there any dynamic way to put sqlite database connection instead of using sqlite:f:\\wamp\\www\\qdr\\protected\\data\\testdrive.db in main.php ?
'db'=>array(
'connectionString'=>'sqlite:f:\\wamp\\www\\qdr\\protected\\data\\testdrive.db',
),
Sql lite is about a single file so you can keep it like this. No dynamic needed. I am sure you are using wamp. Just keep it like this. This is the best solution for you. When you move the project to live server then change accordingly. Sqlite is a single file so do not bother that much.
It depends on what you understand under the vague term dynamic. But you could for example create your custom DBConnection class and override init() there:
class DbConnection extends CDbConnection
{
public function init()
{
// Set $this->connectionString to whatever you want, maybe
// $this->connectionString = 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db';
parent::init();
}
}
You can use this component as db component if you add
'class' => 'DbConnection',
to your main.php.
Note, though, that the init() method is only called the first time when the db component is accessed. So whatever you set as connectionString there will be used for the current request.

accessing the getOptions() of Zend_Application_Bootstrap_Bootstrap

I'm using the default Zend_Application design pattern which loads a zend config ini file automatically in the application bootstrap and I need to ini file's variables across many models and controllers.
Right now, I'm solving it by settings the config object as a key into Zend_Registry:
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
Generally, I don't like using Zend_Registry, as it doesn't offer code auto complete in my IDE, and it's hard to keep track on what I have in the registry namespace.
Is there another way to access the Zend_Application's config ini?
In a controller you should be able to do:
$this->getInvokeArg('bootstrap')->getOptions();
to access the config. For models you really should be passing in the options you need. Otherwise your only choice is really the registry.
You could always initialise it as needed yourself with
$options = new Zend_Config_Ini('/path/to/config.ini',
'config');
Wich is pretty much what the bootstrap does for you. Then you would have autocomplete on $options. But you would have to initialise it everytime you need it.
I think modifying your code to suit autocomplete is not the greatest idea ever. But this is personnal.
If I am not mistaken with Zend Studio 8/9 (maybe 7) you DO have autocomplete even for objects returned by Zend_Registry::get().

How do you work with "global" configuration within ZF?

I'm not really sure what the best way to use global configuration options.
For example, if when a file is uploaded I want to move it in the correct folder.
I can hardcode the path, but it's not really the best thing.
I can use a CONSTANT
I can use config.ini and sets some common config options. Maybe then register a config object in Registry
How do you do? Any advice?
A Zend_Config object in the Registry is the usual method that ZF follows here. Many of the ZF classes can accept Configs, and there's pretty much no better way to deal with it within ZF.
(Just remember, the Registry pattern is little more than a glorified global anyway.)
Create a config.ini, and within it separate your configurations like so:
[development]
;File Upload settings
FileUpload.path = /some/path
[production]
;File Upload settings
FileUpload.path = /production/path
Now somewhere in your Bootstrap.php, you can do this:
$config = new Zend_Config_Ini(
self::$root . '/config/config.ini',
'development'
);
self::$registry->configuration = $config;
And in any controller:
$config = Zend_Registry::get('configuration');
echo $config->FileUpload->path;
I use Zend_Registry::set('foo', '/path/to/correct/folder') once, and then call it whenever I need it with Zend_Registry::get('foo') anytime I need it. Works great!

Is there better way to access configs from Conroller plugin in Zend Framework

Just need get some vals located in application.ini(main ini) in the Controller plugin
I know I can create an instance of Zend_config_Ini but would like to use the existing resource (application.ini is already used in Front Controller)
Use Zend Registry
// when loading the config
Zend_Registry::set('config', $config);
// later, somewhere
$config = Zend_Registry::get('config');
Try:
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
If you now that you will need your configuration values from application.ini in few places around your whole application, you could read it up in bootstrap and store it in registry:
$appConfig = new Zend_Config_Ini('path/to/application.ini');
Zend_Registry::set('applicationConfig', $appConfig);
then later you can always access it:
Zend_Registry::get('applicationConfig')->someValue;
For reasons of testability and decoupling I would not use the Zend_Registry approach. In my opinion the plugin should not know anything about Zend_Config, but instead all options that are needed in the plugin should be injected from outside.
For example, if you want to use the config option "MyOption" in the Plugin Controller_Plugin_MyPlugin you could inject the needed parameters in the Bootstrap class as follows:
public function run()
{
$sopPlugin = $this->getResource('frontcontroller')->getPlugin('Controller_Plugin_MyPlugin');
$sopPlugin->setMyOption($this->getOption('MyOption'));
parent::run();
}
The advantage is that with this solution you avoid dependencies to Zend_Registry (which in fact is just an euphemism for a global variable) in your plugin code.

Categories