Adding custom classes to the application

27 views
Skip to first unread message

DK

unread,
Oct 19, 2011, 8:36:34 AM10/19/11
to Alloy
Ok, so I have 3 custom objects I needed to integrate and decided not
to use the plugin architecture, since these are core functions of the
app.

1. My custom db layer that wraps extends PDO
2. My custom memcache client
3. My custom sessions object which leverages memcached for distributed
sessions (using web server cluster for this app)

I have placed these 3 class files in the app\lib\App folder directly
and decided to instantiate them at the bottom of the init.php script.
Which works fine.

So my questions are:
1. Should I be using a different technique to integrate these classes?
2. If this structure makes sense, is the init.php the right place add
them the Kernel?

Below is my init lines added to the bottom of the init.php script.

#Init MySQL db objects

#Get params from config
$masterHost = $kernel->config('app.database.master.host');
$dbName = $kernel->config('app.database.master.database');
$adapter = $kernel->config('app.database.master.adapter');

#Create master
$tmpdb = new App\Database("$adapter:host=$masterHost;dbname=$dbName"
,$kernel->config('app.database.master.username')
,$kernel->config('app.database.master.password')
);
$kernel->addMethod('master', function() use($tmpdb) {
return $tmpdb;

});

#Create a slave
$slaveHost = $kernel->config('app.database.slave.host');
$tmpdb = new Alloy\Database("mysql:host=$slaveHost;dbname=$dbName"
,$kernel->config('app.database.master.username')
,$kernel->config('app.database.master.password')
);
$kernel->addMethod('slave', function() use($tmpdb) {
return $tmpdb;
unset($tmpdb);

#Init memcache client obj

$tmpCache = new App\Cache($kernel->config('app.memcached.servers'));
$kernel->addMethod('cache', function() use($tmpCache) {
return $tmpCache;

});
unset($tmpCache);

#Init session mgt object

$tmpSession = new App\Session();
$kernel->addMethod('session', function() use($tmpSession) {
return $tmpSession;

});
unset($tmpSession);


Again, thanks much for any input.

DK

Vance Lucas

unread,
Oct 19, 2011, 10:19:45 AM10/19/11
to allo...@googlegroups.com
Looks like you've got the hang of how to customize things in Alloy :).

Plugins are the primary and most recommended way to add functionality to Alloy because they are a nice way to package things up. Since the Kernel is globally available, you can technically interact with it and add methods to it from anywhere, so adding stuff to it in init.php is not really "wrong", it's just not as re-useable.

If you're asking what I would do if it was me - I would add a custom plugin that just adds all of the things you added at the bottom of init.php and put all the lib files in there. That way if I needed to use the same stuff on another project, I would already have it in a nice package in a single directory I could copy over. Plugins are not exclusively for 3rd party stuff - they are just an organizational tool for packaging and re-using code. I use custom app-level plugins on many of my own Alloy apps as well, and when I do, I tend to name them after the name of the app itself. StackboxCMS is a good example:

--
Vance Lucas
http://vancelucas.com

DK

unread,
Nov 13, 2011, 3:50:14 PM11/13/11
to allo...@googlegroups.com
Hey Vance,

Never had a chance till now to reply back.  Just wanted to let you know that I did end up using the Plugin architecture for these classes and it really worked great.  Thanks for the guidance!

I have also been using some of my plugin functionality in App\ControllerAbstract which is really working great, since it's constructor runs first before any of my Module controllers start processing a request.  This allows me to run some global functions like Authorization and such before control goes to the Module.  Also allows me to add some properties and objects to make it a bit more convenient to use them in the controllers.  Sweet.  Thanks again for help.
Reply all
Reply to author
Forward
0 new messages