We're using DependencyInjection and tags to load classes and add them to a chain. I like to expand the functionality with auto loading from a directory.
Directory structure: X\ Y\ MyBundle\ Example\ HelloWorldExample.php
I want to add all classes to the chain which are in the Example directory from all bundles. Something like the Command or Doctrine2 component do with the Command and Entity directories.
I can't find anything on Google (or I'm using the wrong keywords...). Someone can help?
Thanks!
EDIT:
I already use the Dependency Injection component in my Symfony2 application. At this moment I load classes with tagging services (http://symfony.com/doc/current/components/dependency_injection/tags.html) but I want to expand this functionality with auto loading this classes if they are in a specific directory (in the above structure it would be Example).
I don't want to iterate through directories / files.
Composer is perfect for this (and for loading external dependencies) - see https://getcomposer.org/
To start, create a composer.json
file in your root folder:
{
"autoload": {
"psr-0": { "X": "src/" }
}
}
Run composer update
to create the autoload files, then require 'vendor/autoload.php';
in your PHP file.
Given that you have a file src/X/Y/MyClass.php
defining the class \X\Y\MyClass
, the definition should be automatically loaded when you call e.g. $x = new \X\Y\MyClass();
.