In a project of legacy code that I’m working with we have the needing to use some external libraries in order to do some tasks. Also, we planned to refactor the code step by step to introduce good practices, reduce coupling, code duplication, etc.
At this point we had to implement a cron job to send users like a newsletter. This task was the perfect opportunity to create the basic scaffold using Symfony components as it is isolated from the rest of the web application: we consume the data, but almost all the funcionality is new. Then, the first step was to introduce Composer to manage the vendor libraries and autoloading.
Before we start, some points about the project:
- The project doesn’t follow any naming or structure convention. It uses classes and namespacing, but as an example the class names are lowercase and variable names are uppercase.
- The code has an autoloading function, but the intention is to replace it with Composer’s autoloading.
- There are some libraries included in the folder structure.
- There are some files with functions defined between class files.
The project has this folder structure:
1 2 3 4 5
The folders containing classes are
The goal is to configure Composer to:
- Handle the autoloading of existing code.
- Handle the autoloading of new code following PHP-FIG standards.
- Include files with functions.
- Handle vendor libraries.
Then we decided to add the known
vendor folder and
src for the new code.
The first step was configure autoloading. Insted the existing code naming is weird, it respects a class per file and the folder structure corresponding with the namespace, thus we can say that “follows” the PSR-0 for autoloading. For the new code we will follow the PSR-4. Then we can configure autoloading like this:
1 2 3 4 5 6 7 8 9 10
Previous existing libraries
At this point we have some existing libraries included in the code without namespace. Then we have to configure Composer to include them using the
For example we are using the TCPDF library and is stored in
1 2 3 4 5 6 7
Files with functions
Finally to include individual files in the project we use the files autoloading implemented. For example, we have
modulos/sistema/funciones.php that we want to include automatically:
1 2 3 4 5 6 7