I am using Zend_Log, and other required classes, without the MVC framework. I want to add Zend's logging capabilities (and other modules in the future) to my custom classes and I am wondering what the best approach is for doing so.
Right now I have a wrapper for the Zend logger so, presumably, I can access it globally:
My_log::log('Testing', Zend_Log::INFO);
Should I be adding this code to each method in my classes that I want to log? Should I not create logs inside my classes? Is there a smarter way?
I appreciate the help,
DC
Dependency injection container seems like a great solution if your app can be integrated. All static calls cause issues in testing environment.
have look around this doc
http://components.symfony-project.org/dependency-injection/trunk/book/04-Builder
Worst case i would create a static getter like My_Log::get()->error("message"); the only point is that now you will be able to easily fix the test environemnt to make get return a fake instance. All your My_Log needs is a setLogger($logger) which will replace the static instance with a mock or whatever. Any way static calls are bad :/ if possible try to decouple classes so they would depend on as few classes as possible. Even injecting logger manually into your class constructor is a better idea. If you have MVC action plugin or a base controller could provide lazy loading getLogger() so your code could do $this->getLogger()->error('...');
art
Related
I am looking for a php implementation or a design pattern something like this (just a very basic skeleton example):
namespace Contract {
interface Application {...}
interface EntryPoint {...}
}
namespace RestApi {
class Module {
/** #return Contract\EntryPoint */
public function getEntryPoint(Contract\Application $application){...}
}
class EntryPoint implements Contract\EntryPoint {...}
}
namespace BusinessLogic {
class Module {
/** #return Contract\Application */
public function getApplication(){...}
}
class Application implements Contract\Application {...}
}
$dependencyResolver = new DependencyResolver();
$dependencyResolver->parse(new RestApi\Module());
$dependencyResolver->parse(new BusinessLogic\Module());
$dependencyResolver->invoke(function (Contract\EntryPoint $entryPoint){
$entrypoint->handleRequest();
});
I want to loose couple every modules I am using in my application, so I intend to design interfaces, maybe abstract classes with validation to make a well defined interface for every module type. I haven't found a solution for this problem, ppl usually inject things with IoC container, which does not tell anything about the common interface between two modules...
Do you know a design pattern which solves this problem, or a de factor standard php implementation/framework for this?
note: IoC container is not a solution, I want to inject the dependencies with well defined interfaces, not to pull them from a DI container or service locator... I don't want my modules to know anything about how they get their dependencies...
edit:
I updated my question because I don't think my code was obvious for everyone. A created a class diagram to ease the understanding of it:
Okay, so this diagram contains a description of a single environment. We usually use different environments for example: test, development, production, etc... Each of these environment contains different modules, for example by testing and developing we usually turn off email sending, so one of the modules contains a mock php mailer in those environments... As you see by cross-module dependency the classes depend on contracts, not directly on each other. This way the code of the modules is loose coupled...
I intend to describe this dependencies (for example with annotations) and inject them on an automated way somehow. This is a difficult task, because by PHP I have to load only the classes which are necessary to handle the request. For example:
So I have to use lazy load somehow, for example I could inject factories, but I don't like that idea, I want to inject the dependency itself, not a factory...
Be aware that the contract interface does not know anything about its implementations, so I have to publish somehow those implementations by every module and find them from the other modules... Most of the DI container implementations solve that problem, but I don't want to inject factories or DI containers into my modules. I want them to depend on the contracts only and nothing else...
Matthieu Napoli recommended to use just a single IoC container with different config by each of the environments, but I don't see how this would solve my problem. That DI container would instantiate every class with cross-module dependency, so I would move the whole config of every modules into a huge main config file. Even by a simple project I'd have about 20 classes (languages, users, identification-factors, user-identification-factors, contacts, user-contacts, roles, user-roles, permissions, role-permissions, articles, comments, etc...) and at least 3 modules (presentation layer, business logic layer, data access layer). So that main config would contain the instantiation of at least 60 classes from different modules... That would be pretty hard to maintain and I am almost certain that it would result a lot of code repetition by configs of different environments... Yes maybe splitting the config files can reduce the pain, but I guess I wont know the advantages and drawbacks of this solution until I start to use it on a complicated project. Another problem with this approach that how should I implement the lazy load without injecting the IoC container itself into every class with cross-module dependency? I think I need proof or example code, that this approach really works well by this problem domain...
Currently I am thinking on something similar than require.js does with AMD javascript modules, but that injects the IoC container as well by lazy load. You have to use the require("moduleName") from inside the modules if you want to load a dependency which you don't want to use in your code, just in case when it's really necessary. Ofc that require is just a sugar syntax of container.get("moduleName")... Currently I don't see how to solve that problem. I think every of my modules should have one or more DI containers, call them module containers. Those module containers can handle the cross-module dependencies. By lazy load the module containers would pull down the cross-module dependencies of each class from a main container, which would automatically register every module container and every class instantiated by them. In this scenario only the module containers would know about the main container if I cannot solve lazy load without injecting a factory or a DI container or a service locator... Ofc this is the last resort, I think I can do the lazy load somehow without injecting the main container into the module containers. Or at least I can do it somehow with a sugar syntax, for example:
class ModuleContainer {
public function setCrossDomainDependency(Contract\Dep $crossDomainDependency){
//...
}
/** #return Contract\Dep */
public function getCrossDomainDependency(){
//...
}
}
My purpose to use the same ModuleContainer instance regardless of having a MainContainer. I'll check the reflection api, maybe I am able to override the getCrossDomainDependency somehow at runtime. If not then I think the only solution to inject factories or the main container... Nah but this is my solution...
How would you solve this problem?
A IoC container is exactly the solution.
I want to inject the dependencies with well defined interfaces, not to pull them from a DI container or service locator
Then don't use the container as a service locator. That's exactly what a DI container is meant for.
Here is your example using PHP-DI:
$containerBuilder = new ContainerBuilder();
$container = containerBuilder->build();
// Configure RestApi\EntryPoint to use for Contract\EntryPoint
$container->set(Contract\EntryPoint::class, \DI\link(RestApi\EntryPoint::class));
// Configure BusinessLogic\Application to use for Contract\Application
$container->set(Contract\Application::class, \DI\link(BusinessLogic\Application::class));
// Run
$entryPoint = $container->get(Contract\EntryPoint::class);
$entryPoint->handleRequest();
Of course my example here is not perfect. I would suggest to separate the configuration part from the execution part. I would put the configuration in a configuration file (look here for how).
Important: Yes, in that example I fetch something from the container. That is exceptional. That should happen only once or twice in your application.
You have to use the container at some point, at the root of your application (i.e. the entry point of the app), to construct the object graph (or dependency graph).
I am not suggesting that you call get() on the container everytime you need a dependency. I am suggesting that you use dependency injection instead, and that you call get() only at the entry point of the application.
Example of defining with a factory:
$container->set(Contract\EntryPoint::class, \DI\factory(function (Container $c) {
return new RestApi\EntryPoint($c->get('some.parameter'));
}));
I have read a lot of SO topics about this already, but still haven't found (or have been able to create) a proper answer.
I am working on a small MVC framework and I want some global class/object that I can call from my controllers (but maybe models too).
There are a couple of routes I can take:
Global variable
Static class/Registry
Dependency injection
The internet seems to agree that the DI is the best route to take. I think I have grasped the idea, but am not comfortable yet. So I want to throw in some background information; I will probably be the only one working on the project, it is a small project, all my controllers extend the main Controller (so I could just load one library like class there).
As a concrete example, I want to have an array of categories. So I started out with putting that array in the CategoryController. But now I noticed I kinda want to use that array in my frontview and in ProductController as well. Obviously I don't want to load all of CategoryController into ProductController.
You could also say I could put that array in some kind of configuration or settings file, because of the simpleness of this example, but that's why it's an example. I will probably expand on it with more functionality.
So to summarize: In PHP (specifically inside a MVC model) how can you give your classes (mainly Controllers) access to some kind of common class or other sharable functionality.
Your controllers are created by "something" (usually a front controller). So when the controller is created, you could inject a dependency injection container.
And in your configuration/bootstrap (before the controller is created), you should add you categories to the container.
That way you can access the categories from every controller.
Please note that this is a simple example that doesn't totally fit the spirit of dependency injection. The best solution would be to inject directly the categories (instead of injecting the container). But that can become a lot of work if you generalize that pattern (lots of dependencies to handle in your front controller).
A solution would be to use a dependency injection framework that could do that for you.
For example I work on a DI container that lets you inject stuff with annotations (PHP-DI), but there are several other libraries for DI so you have a choice.
My 2 cents:
In a small self-made mini-framework I have done some time ago, I have created a global singleton class named Application, and anything/everything which should be accessible from anywhere/everywhere was a property or method of this class.
In my case, there was a $db property for database access, a $user property to access the user data and methods, an $input property for a "powered" $_REQUEST access, and so on.
Of course, you have many other options, suitable for different scenarios. This approach simply worked fine for me on that occasion.
Now, if you want to access a controller from another controller, this really sounds strange. This "thing" that you want to access should be a model class, a library class, or anything else, but it should not be "locked" inside a controller class. Indeed, the controller should be "as thin as possible", and focus on calling the appropriated methods from other classes, based on the user input (request) and then calling some output method to generate and send the answer (response).
Finally, although I've read some criticism and complaints about it (as well as praises too), I do make use of static methods a lot, mainly for classes which are more "helpers" than anything else.
I'm working on building a PHP framework that behaves according to MVC principles and utilizes dependency injection. I think I have the front-controller part down; there is a working router that instantiates a controller instance and calls the appropriate action based on the requested URI.
Next up is dependency injection. I want to implement a Container that resolves dependencies using reflection. In doing so, I think I'm running into a problem with my controllers.
There are a number of what I call "system dependencies" that need to be available to derived controller classes. I haven't actually created all these dependencies yet, but it seems sensible that controllers have access to services like an InputProvider (to encapsulate get/post params or command line arguments), and maybe an Output dependency.
Ideally, I'd use the framework's Container to inject these dependencies into the constructor of the controller - but this is where I run into problems.
If I use constructor injection for system dependencies in the controller, then I'm forcing derived controllers to manage the base controller's dependencies if they implement a constructor of themselves. That doesn't seem to be the most user-friendly.
The other option is to use setter injection for system dependencies, but then derived controllers won't have access to these system dependencies if they should have need of them in their constructor.
The only solution I see that offers the best of both worlds is to make my controllers singletons. They'd have a private constructor so I can safely use setter injection without worrying about the constructors of derived classes. Instead, there would be an overridable initialize() method (assuming I get method injection working somehow), that basically fulfills the role of constructor (as in, an initializer for the derived controller). This way, constructor injection is replaced by method injection in the initialize() method, where all system dependencies would be available, without requiring the derived controller to manage them.
But then, a quick google search seems to unanimously say that singleton controllers are bad practice. I'm very unsure on how to proceed. I am probably overengineering this, but apart from wanting my application to be future-proof and maintainable, I also see it as a little exercise in applying best practices, so I'd like to do things "properly".
I think the best practice in this case would be to pass the responsibility of managing the required system dependencies to the derived controller. A dependency should probably only be instantiated if the derived controller actually has need of it. Why inject an InputProvider in the base controller if it is possible that a derived controller is never even going to use it? But at the same time, I keep coming back to user friendliness, and how nice it is to simply always have a $this->input member available, such as in a framework like CodeIgniter.
I highly appreciate any and all contributions to my dillemas. I also apologise for the wall of text, but I couldn't think of any code examples that would make the job of explaining any easier, since it's all so abstract to me right now!
Sincerely,
A severely torn individual
Several solutions are possible:
forbid controllers to use __construct(): make it private public final, and make controllers override something like init() and call it from the constructor. Then the constructor would inject all the dependencies (reflection? other stuff?), so that they are all ready in init().
you can use an existing DI library like PHP-DI (disclaimer: I work on that) that will allow you to define dependencies but have them available in the constructor (magically, yes).
Something like that:
<?php
use DI\Annotations\Inject;
class FooController {
/**
* #Inject
* #var Bar
*/
private $bar;
public function __construct() {
// The dependency is already injected
$this->bar->sayHello();
}
public function setBar(Bar $bar) {
return $this->bar = $bar;
}
}
For example that is how I work with Zend Framework 1. I can't use the constructors, so I inject into properties. Here is the ZF1 integration project.
I'm designing a class that has two dependencies. One of the dependency classes has been written and tested. The other has not yet been written.
It has occurred to me because the remaining dependency will be written to facilitate the class that will use it, that I should write the latter first, and design the interface of the former as I go along, learning what it should do.
That seems to me to be a great way to make code. After all, as long as the main class gets a mock in its constructor, I can write it and test it without it being aware that its dependency doesn't exists, then I can create the dependency once I am sure I know what I need.
So: how do I do this? Create a skeleton class that I modify as I go along. Perhaps something like:
class NonExistantSkeleton
{
public function requiredMethod1()
{
}
public function newlyDiscoveredRequirement()
{
}
}
and then mock it using PHPUnit, and setting up stubs, etc, to keep my class under development happy?
Is this the way to go?
It seems like a nice way to develop code - and seems to me to make more sense than developing a dependency, without really knowing for sure how it's going to be used.
In short:
Yes. At least thats what I'm doing right now.
Longer Version:
If the expected collaborators of your class don't exist at the point in time where you need them in your tests for the class you are building you have a few options:
Mock non existing classes (which phpunit can do)
Create class skeletions and mock those
Just create interfaces and get mocks for those (which phpunit can do too)
Maybe you don't need any of the above depending on the object
If you programm against an interface anyways than all you need to do is to create that interface and tell PHPUnit to create a stub/mock from it
+No new class without a test
+Using interfaces when appropriate is considered nicer/better than just hinting against classes
When mocking non existing classes you get some drawbacks that I don't like:
-High mock maintenance cost
-Chaning the methods on that classes is slow and tedious
-If you created the class you should rework the mocks again
so I'd advice against that.
The middle way would be to just create the empty class skeleton with its method and use those for mocking.
I quite like that way in cases where there is no interface to hint against as It is fast and creates stable test code.
Having barebone classes with public apis, for me, is no violation of TDD.
There are classes you don't need to mock.
Data transfer objects and Value Objects can always be created anywhere using the new in your production code so your tests also can just the the real objects.
It helps to keep your tests a little cleaner as you don't need to mock/expect a lot of getter/setter methods and so on.
If you follow a test-driven development methodology then the usual approach is as follows:
Figure out what your classes are meant to do, and what their public-facing APIs should be.
Implement "empty" classes that consist of nothing but the public methods signitures with empty bodies (as you have done in the code example you gave).
Work out an implementation strategy. This means working out which classes are dependant on each other and implementing them in an order that means that dependant classes aren't implemented until the classes it depends on are finished, or at least sufficiently functional to develop against. This means doing the classes with no dependencies first, then the classes that depend only on the completed classes, and so on.
Write your tests. It's possible to write the tests now because you know what the black box for your classes look like, what they need to take as input and what they're supposed to return as output.
Run the tests. You should get 0% success, but also 100% code coverage. This is now your baseline.
Start to implement your classes according to your implementation strategy. Run your unit tests from time to time during this process, say once you've got a class completed, to make sure that it meets its specification as laid down in the unit test. Ideally, each test should show an increase in test passes whilst maintaining 100% code coverage.
EDIT: As edorian pointed out, PHP interfaces are a huge help here because PHPUnit can generate mocks and stubs from interfaces as well as from classes. They're also an excellent tool in reducing coupling and improving substitutability in general. They allow you to substitute any class that implements the expected interface, instead of just subclasses of the expected class.
How to solve the problem of composing a Controller class in PHP, which should be:
easily testable by employing Dependency Injection,
provide shared objects for end programmer
provide a way to load new user libraries
Look down, for controller instantiation with a Dependency injection framework
The problem is, that derived Controllers may use whatever resources the programmer wants to (eg. the framework provides). How to create a unified access to shared resources (DB, User, Storage, Cache, Helpers), user defined Classes or another libraries?
Elegant solution?
There are several possible solutions to my problem, but neither one looks to be a elegant
Try to pass all shared objects by constructor? (may create constructor even with 10 placeholders)
Create getters, settters? (bloated code) $controller->setApplication($app)
Apply singletons on shared resources? User::getInstance() or Database::getInstance()
Use Dependency Injection container as a singleton for object sharing inside the controller?
provide one global application singleton as a factory? (this one looks very used in php frameworks, hovewer it goes strongly against DI principles and Demeter's law)
I understand, that creating strongly coupled classes is discouraged and banished for :), however I don't know how this paradigm applies to a starting point for other programmers (a Controller class), in which they should be able to access shared resources provided to the MVC architecture. I believe, that breaking up the controller class into smaller classes would somehow destroy the practical meaning of MVC.
Dependency Injection Framework
DI Framework looks like a viable choice. However the problem still persists. A class like Controller does not reside in the Application layer, but in the RequestHandler/Response layer.
How should this layer instantiate the controller?
pass the DI injector into this layer?
DI Framework as a singleton?
put isolated DI framework config only for this layer and create separate DI injector instance?
Are you developing a framework yourself? If not, your question does not apply, because you have to choose from already existing frameworks and their existing solutions. In this case your question must be reformulated like "how do I do unit testing/dependency injection in framework X".
If you are developing a framework on you own, you should check first how already existing ones approach this issue. And you must also elaborate your own requirements, and then just go with the simplest possible solution. Without requirements, your question is purely aesthetic and argumentative.
In my humble opinion the simplest solution is to have public properties which initialize to defaults provided by your framework, otherwise you can inject your mocks here. (This equals to your getters/setters solution, but without the mentioned bloat. You do not always need getters and setters.) Optionally, if you really need it, you may provide a constructor to initialize those in one call (as you suggested).
Singletons are an elegant solution, but again, you must ask yourself, is it applicable in your case? If you have to have different instances of the same type of object in your application, you can't go with it (e.g. if you wish to mock a class only in half of your app).
Of course it is really awesome to have all the options. You can have getters/setter, constructors, and when initialization is omitted, default are taken from a singleton factory. But having too many options when not needed, is not awesome, it is disturbing as the programmer has to figure out which convention, option and pattern to use. I definitely do not want to make dozens of design decisions just to get a simple CRUD running.
If you look at other frameworks you will see that there is no silver bullet. Often a single framework utilizes different techniques depending on the context. In controllers, DI is a really straightforward thing, look at CakePHP's $helpers, $components variables, which instruct to inject appropriate variables into the controller class. For the application itself a singleton is still a good thing, as there is always just a single application. Properties less often changed/mocked are injected utilizing public properties.
In case of an MVC, subclassing is perfectly viable option as well: just as AppController, AppView, AppModel in CakePHP. They are inserted into the class hierarchy between the frameworks's and all your particular Controller, View and Model classes. This way you have a single point to declare globals for your main type of classes.
In Java, because of dynamic class loaders and reflection, you have even much more options to choose from. But on the other hand, you have to support much more requirements as well: parallel requests, shared objects and states between worker threads, distributed app servers etc.
You can only answer the question what is right for you, if you know what you need in the first place. But actually, why do you write just another new framework anyway?
Singletons are frowned upon when Dependency Injection is viable (and I have yet to find a case where a Singleton was necessary).
More than likely you will have control of instantiation of controllers, so you can get away with the mentioned $controller->setApplication($application), but if necessary you can use static methods and variables (which are far less harmful to the orthogonality of an application than Singletons); namely Controller::setApplication(), and access the static variables through the instance methods.
eg:
// defining the Application within the controller -- more than likely in the bootstrap
$application = new Application();
Controller::setApplication($application);
// somewhere within the Controller class definition
public function setContentType($contentType)
{
self::$application->setContentType($contentType);
}
I have made of a habit of separating static and instance properties and methods (where necessary, and still grouping properties at the top of the class definition). I feel that this is less unwieldy than having Singletons, as the classes still remain quite compact.
How about refactoring?
Granted that was not one of your options, but you state the code is a largely coupled class. Why not take this time and effort to refactor it to more modular, testable components?
As far as I understand, the Application class of yours should be the dispatcher. If so, I would rather use the controller constructor to pass an instance of the Application, so the controller would know who's invoking it. At later point if you want to have a different Application instance depending on whether the code is invoked from within CLI, you can have an ApplicationInterface which the Application\Http and Application\Cli would implement and everything would be easy to maintain.
You could also implement some factory pattern to get a nice implementation of the DI. For example, check the createThroughReflection method here: https://github.com/troelskn/bucket/blob/master/lib/bucket.inc.php
I hope this makes sense.
Regards,
Nick
You could also use a ControllerFatory in which you would give to your Application or Router/Dispatcher
Sou you could call $controllerFactory->createController($name);
Your Application would have no idea how to create your controllers the Factory would. Since you ca inject your own ControllerFactory in to your DI container you can manage all dependencies you want depending on the controller.
class ControllerFactory {
public function __construct(EvenDispatcher $dispatcher,
Request $request,
ResponseFactory $responseFactory,
ModelFactory $modelFactory,
FormFactory $formFactory) {
...
}
public function createController($name = 'Default') {
switch ($name) {
case 'User':
return new UserController($dispatcher,
$request,
$responseFactory->createResponse('Html'),
$modelFactory->createModel('User'),
$formFactory->createForm('User'),...);
break;
case 'Ajax':
return new AjaxController($dispatcher,
$request,
$responseFactory->createResponse('Json'),
$modelFactory->createModel('User'));
break;
default:
return new DefaultController($dispatcher, $request, $responseFactory->createResponse('Html'));
}
}
}
So you just need to add this factory in your DI container and pass it to your Application.
Whenever you need a new controller you add it to the factory and if new dependencies are required you give them to the factory through your DI container.
class App {
public function __construct(Router $router,Request $request, ControllerFactory $cf, ... ) {
...
}
public function execute() {
$controllerName = $this->router->getMatchedController();
$actionName $this->router->getMatchedAction();
$controller = $cf->createController($controllerName);
if(is_callable($controller, $actionName)) {
$response = $controller->$action(request);
$response->send();
}
}
}
This is not production code, I haven't tested, but this is how you decouple your controllers from your application. Notice here though that there is one bad coupling here because my controller return's a response and I execute the response in the App. But like I said this is just a small example.
It is usually a good idea to pass factories for Models, Forms, and Controllers to their respective parents, because you will end up loading all your object Graph at bootstrap time wich is really bad and memory consuming.
I know this answer was already approved, but it's my 2 cents on the subject
There is a good article on the subject
http://miller.limethinking.co.uk/2011/07/07/dependency-injection-moving-from-basics-to-container/