TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   TalkPHP Developer Team (http://www.talkphp.com/talkphp-developer-team/)
-   -   Some contributions (Registry, application configuration) (http://www.talkphp.com/talkphp-developer-team/3884-some-contributions-registry-application-configuration.html)

Ross 01-17-2009 02:35 PM

Some contributions (Registry, application configuration)
 
I've got some classes that I was previously developing for a personal framework but I think would make a good addition to the app that I'd like to contribute.

Registry
This is a common solution to the globals anti-pattern.

Basically, rather than keeping variables as globals and risk overwriting them, you have a singleton instance of this class created in your bootstrap file. This remembers itself so everytime you call it it knows what's been set in it's datastore.

Class code (pastebin'ed as it's fairly long)
Example usage:
PHP Code:

$registry Registry::getInstance();
$registry->configuration $config// Some config array/object
unset($registry);

// later
$registry->getInstance();
var_dump($registry->configuration);
$dbAdapter = new Database($registry->configuration->database);
$registry->database $dbAdapter;
unset(
$registry); 

This also includes some static methods, such as a class/interface autoloader.

Configuration
This isn't the same as the other config class which appears to load options from the database.

This is a two-parter. It has an abstract base class that is extended based on the file format you want to retrieve data from.

It allows you to use constants in configuration files. However the configuration files must be placed above the document root otherwise they will be publically viewable (and since they will likely contain database data this is bad!). See below.

Abstract Class
INI File extension
Example
PHP Code:

$configuration = new Phlox_Config_Ini(APPLICATION_PATH '/config/app.ini');
var_dump($configuration->database); 

Sample INI File
Code:

[database]
adapter = 'MySQL'
params.dbname = 'phlox'
params.user = 'phlox_user'
params.password = 'password'
params.host' = 'localhost'

Files above document root
Really, all of the includes, etc. should be above the Document Root and publically accessible files (index.php, /js/, /css/) should be the only things below it. This is available on nearly all hosts (including shared hosts - all of mine have done this).

What I suggest is that we have a public directory for front-facing files.

tony 01-17-2009 09:27 PM

This is quiet handy. A singleton to get a main registry and constants saved in a separate file (language agnostic).
Thanks man, never thought of this before.

Ross 01-17-2009 09:29 PM

Quote:

Originally Posted by tony (Post 21323)
This is quiet handy. A singleton to get a main registry and constants saved in a separate file (language agnostic).
Thanks man, never thought of this before.

It's more configuration than constants - for example database details. The handy thing is the configuration and the database adapter can be dumped in the registry and used all over the place :)

Committed Registry (and a bootstrap file). --Working on fixing a few flaws in Config before I commit that.-- Config classes committed.


All times are GMT. The time now is 02:47 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0