.. _di.preload:

==============
Class Preload
==============

InterMix supports generating a **class preload file** for use with PHP's
``opcache.preload`` feature — which can dramatically reduce **cold-start time**
in production by preloading frequently used classes into memory at startup.

-------------------------
Generate the Preload File
-------------------------

.. code-block:: php

   use Infocyph\InterMix\DI\Support\PreloadGenerator;

   $generator = new PreloadGenerator();
   $generator->generate($c, __DIR__ . '/preload.php');

This will create a preload script that includes all known classes, definitions,
and registered services from the container instance.

------------------------
Example Output Contents
------------------------

The generated file looks like:

.. code-block:: php

   <?php

   // Generated by InterMix PreloadGenerator
   require_once '/path/to/vendor/SomeService.php';
   require_once '/path/to/app/Logger/FileLogger.php';
   require_once '/path/to/app/Service/UserService.php';
   // ...

No logic — just ``require_once`` entries for reflection-resolved classes.

-----------------------------------
Use with ``opcache.preload`` in PHP
-----------------------------------

In your ``php.ini``:

.. code-block:: ini

   opcache.preload=/var/www/html/preload.php
   opcache.preload_user=www-data

This causes PHP to preload those classes **before** serving any requests,
eliminating file system lookups and improving performance.

-----------------------------------
When to Regenerate Preload File? 🔁
-----------------------------------

You should regenerate the preload file when:

- You register new classes or services
- Definitions change significantly
- Deploying to production with updated dependencies

Automate it as part of your deployment pipeline or cache warm-up.

--------------------------
Advanced Customization 🛠️
--------------------------

You may extend or modify the preload generation behavior by subclassing
:php:class:`Infocyph\\InterMix\\DI\\Support\\PreloadGenerator`.


Next stop » :doc:`debug_tracing`
