I read about your project in March 2013 Linux Magazine France. I found the concept brilliant and like to use it in a small project of mine. Since I am just a programming enthusiast, I do not understand some concepts or, at least, don't know how to implement them. I read some articles about the "Factory pattern" but still can't understand how FigDice need it to work when using Feeds. Could you please point me to some working code I could study?
Thanks in advance.
Martin
Last edit: Gabriel Zerbib 2013-03-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Let's consider that your Feed class MyFeed must read data from a database.
Feed class
First, your Feed class must have a public method which enables the injection of the Database object (PDO, direct credentials, mysqli resource: it can be anything you want, depending on the way you code your DB interactions). The Feed instance will just assume that this has been prepared beforehand, from outside.
Now, you will tell your FIG_View how to instantiate MyFeed on demand:
you do this through a Feed Factory.
___File: MyDatabaseAwareFeedFactory.php ___
:::PHP53<?phpclassMyDatabaseAwareFeedFactoryimplementsFIG_FeedFactory{publicfunctioncreate(/*string*/$className,array$attributes){//Your Factory can be responsible for several// Feed classes, based on their namesif('MyFeed'==$className){$feed=newMyFeed();$feed->setDatabase(/* everything you need to make the Feed class aware of the DB */);return$feed;}//Your factory should serve in the same way//the instances of various Feed class//which belong to a same family (e.g. Feeds that//read data from one same database).//If your Factory class was requested a Feed// which it does not know to handle:returnnull;}//You call this method yourself, in your controller,//before passing this Factory to the View.//By this method you make your Factory keep track//of the Database, so as to make it possible to//propagate it to the future Feed instances that//this Factory will create.publicfunctionsetDatabase(...){...}}
Controller
Finally, your controller (which owns the main access to database) will invoke the view after registering the Feed Factory.
The Feed Factory is given access to DB through your special method, and in turn it will be able to pass it the the feeds later.
Hi Gabriel,
I read about your project in March 2013 Linux Magazine France. I found the concept brilliant and like to use it in a small project of mine. Since I am just a programming enthusiast, I do not understand some concepts or, at least, don't know how to implement them. I read some articles about the "Factory pattern" but still can't understand how FigDice need it to work when using Feeds. Could you please point me to some working code I could study?
Thanks in advance.
Martin
Last edit: Gabriel Zerbib 2013-03-24
Hi Martin,
Thank you for your comments.
Below is an example of how to use the Factory pattern with Feeds in FigDice.
Suppose that you want to render this View:
___File: myView.xml ___
Let's consider that your Feed class MyFeed must read data from a database.
Feed class
First, your Feed class must have a public method which enables the injection of the Database object (PDO, direct credentials, mysqli resource: it can be anything you want, depending on the way you code your DB interactions). The Feed instance will just assume that this has been prepared beforehand, from outside.
___File: MyFeed.php ___
Feed Factory
Now, you will tell your FIG_View how to instantiate MyFeed on demand:
you do this through a Feed Factory.
___File: MyDatabaseAwareFeedFactory.php ___
Controller
Finally, your controller (which owns the main access to database) will invoke the view after registering the Feed Factory.
The Feed Factory is given access to DB through your special method, and in turn it will be able to pass it the the feeds later.
I hope this helps!
Regards,
Gabriel
Last edit: Gabriel Zerbib 2013-03-25