Swift and my own extension loading
Posted: Thu Jul 13, 2006 9:00 am
Alright here is what I am trying to do. I am making my own small framework and I want to be able to load extensions into a class which essentially is like loading feyd's SHA256 and d11wtq's Swift and whatever other 3rd party libraries I might want to use. Now this was easy with feyd's stuff (although I had to make it php5 compatable) but d11wtq's style of stuff is difficult. Here is what I want to be able to do:
and then I want to be able to use $mailer just like the documentation says to. Seams easy enough if I just put the file in the first array to require, then the class to initiate in the second array for each file, then the params that I would put in when initiating the class in the 3rd array. The first parameter there 'Swift' is just the folder its located in.
Now here is where the problems come in! Swift has to first require swift.php, then the smtp_connection.php BUT it then has to initiate the smtp_connection class before the swift class while passing the instance of the smtp_class to the swift initiation...
maybe that did not make sence but look at it from here: http://www.swiftmailer.org/documentation/5#view
now here is what I tried with my LoadExtension method:
but obviously that does not work because of the order of things changes and the params are instances of the other classes.
Now this is not picking on d11wtq because if I was just going to use it once in one place then I would just do it all manually, no problems but this is more of a situation that might come up often and I don't want to have to copy / paste a whole bunch of code every time, I just want to be able to use my handy LoadExtension() method and if something needs changed then it's easier.
What my question is: is there a clean, simple way to do such a thing as what I want to do? Does anyone have any ideas on how to make something like my LoadExtension() method work with different libraries and whatnot so no matter what kind of structure (within reason) it requires to load it, I still can with the same method? OR should I just give up and just copy/paste the code every time?
Code: Select all
<?php
class MailerController extends Base
{
public function __construct()
{
}
public function Mailer()
{
$mailer = $this->LoadExtension('Swift', array('Swift', 'Swift/Swift_SMTP_Connection'), array('Swift', 'Swift_SMTP_Connection'), array('', 'smtp.YOUR-ISP.com'));
}
}
?>Now here is where the problems come in! Swift has to first require swift.php, then the smtp_connection.php BUT it then has to initiate the smtp_connection class before the swift class while passing the instance of the smtp_class to the swift initiation...
maybe that did not make sence but look at it from here: http://www.swiftmailer.org/documentation/5#view
now here is what I tried with my LoadExtension method:
Code: Select all
public function LoadExtension($extension, $files, $classes, $params)
{
if (is_array($files))
{
foreach ($files as $key => $val)
{
require_once './Extensions/'.$extension.'/'.$val.'.php';
if ('' !== $classes[$key])
{
if ('' !== $params[$key])
{
//really don't know if this will even work the way I want it to
$tmp[] = new $classes[$key]($params[$key]);
}
else
{
$tmp[] = new $classes[$key];
}
}
}
}
else
{
require_once './Extensions/'.$extension.'/'.$files.'.php';
}
}Now this is not picking on d11wtq because if I was just going to use it once in one place then I would just do it all manually, no problems but this is more of a situation that might come up often and I don't want to have to copy / paste a whole bunch of code every time, I just want to be able to use my handy LoadExtension() method and if something needs changed then it's easier.
What my question is: is there a clean, simple way to do such a thing as what I want to do? Does anyone have any ideas on how to make something like my LoadExtension() method work with different libraries and whatnot so no matter what kind of structure (within reason) it requires to load it, I still can with the same method? OR should I just give up and just copy/paste the code every time?