I'm currently trying to implement a simple dependency injection container that uses XML to define the class dependencies. The questions I have are mainly related to design.
Here is an example XML schema that I am thinking about using:
<?xml version="1.0">
<classes>
<class name="MySQLDatabase">
<namespace>MyLib\Database\</namespace>
<dependency type="constant">my_db_username</dependency>
<dependency type="constant">my_db_password</dependency>
<dependency type="constant">my_db_database</dependency>
</class>
<class name="LoginManager">
<namespace>MyLib\Authentication\</namespace>
<dependency type="class">MySQLDatabase</dependency>
</classes>
</classes>
My first question is, does anyone foresee any problems with a schema similar to the above?
My second question is, how should I go about translating this into an my definitions array? This question isn't about how to parse the XML, rather the design of the array that will hold the information once parsed.
If I was coding this in Java, I was thinking of something like this, but I'm not sure if this is too bulky and how this would translate into PHP, which doesn't seem to have the same amount of data structures as Java does.
class DIClass {
public HashMap<String, ClassDefinition> definitions;
// example
// definitions = map of the classes below
// ...
}
class ClassDefinition {
private String name;
private String namespace;
private List<Dependency> dependencies;
// example
// name = MySQLDatabase
// namespace = MyLib\Database\
// dependencies = list of the class below, ordered
// ...
}
class Dependency {
private String name;
private String type;
// example
// name = my_db_username
// type = constant
// ...
}
My last question is, what about adding a definition for persistence? For example, the database class should be a singleton. Should I just add an extra <persis />
tag into the class definition in the XML file?
Thanks for looking.