[<< previous](11-templating.md) | [next >>](13-refactoring.md) ### Configuration In the last chapter we added some more definitions to our dependencies.php in that definitions we needed to pass quite a few configuration settings and filesystem strings to the constructors of the classes. This might work for a small projects, but if we are growing we want to source that out to a more explicit file that holds all the configuration valuse for our project. As this is not a problem unique to our project there are already a some options available. Some projects use [.env](https://github.com/vlucas/phpdotenv) files, others use [.ini](https://www.php.net/manual/de/function.parse-ini-file.php), there is [yaml](https://www.php.net/manual/de/function.yaml-parse-file.php) as well some frameworks have implemented complex Readers for many configuration file formats that can be used, take a look at the [laminas config component](https://docs.laminas.dev/laminas-config/reader/) for example. As i am a big fan of writing everything in php, which gives our IDE the chance to autocomplete our code better I am quite happy the PHP8 gives us some tools to achieve easy to use configuration via php. You can take a look at [this blogpost](https://stitcher.io/blog/what-about-config-builders) to read about some considerations on that topic before moving on. Lets create a 'Settings' class in our './src' Folder: ```php filePath; } } ``` If we later want to use yaml or ini files for our Settings we can easily write a different provider to read those files and craft a settings object from them. As we have now created a completely new Namespace and Folder and our SettingsProvider is all alone we could add another factory for our Container because everyone should have a Friend :) ```php settingsProvider->getSettings(); $dependencies = require $settings->dependenciesFile; $dependencies[Settings::class] = fn () => $settings; $builder->addDefinitions($dependencies); return $builder->build(); } } ``` For this to work we need to change our dependencies.php file to just return the array of definitions: And here we can instantly use the Settings object to create our template engine. ```php fn (ResponseFactory $rf) => $rf->createResponse(), ServerRequestInterface::class => fn (ServerRequestFactory $rf) => $rf::fromGlobals(), Now::class => fn (SystemClockNow $n) => $n, Renderer::class => fn (Mustache_Engine $e) => new MustacheRenderer($e), MLF::class => fn (Settings $s) => new MLF($s->templateDir, ['extension' => $s->templateExtension]), ME::class => fn (MLF $mfl) => new ME(['loader' => $mfl]), ]; ``` Now we can change our Bootstrap.php file to use the new Factories for the creation of the Initial Objects: require __DIR__ . '/../vendor/autoload.php'; ```php ... error_reporting(E_ALL); $settingsProvider = new FileSystemSettingsProvider(__DIR__ . '/../config/settings.php'); $container = (new SettingsContainerProvider($settingsProvider))->getContainer(); $settings = $settingsProvider->getSettings(); $whoops = new Run; if ($settings->environment === 'dev') { $whoops->pushHandler(new PrettyPageHandler); } else { $whoops->pushHandler(function (Throwable $e): void { error_log('Error: ' . $e->getMessage(), $e->getCode()); echo 'An Error happened'; }); } $whoops->register(); ... ``` Check if everything still works, run your code quality checks and commit the changes before moving on the the next chapter. [<< previous](11-templating.md) | [next >>](13-refactoring.md)