PHP code example of infocyph / intermix

1. Go to this page and download the library: Download infocyph/intermix library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

infocyph / intermix example snippets




use function Infocyph\InterMix\container;

$c = container();
$c->definitions()->bind('now', fn () => new DateTimeImmutable());

echo $c->get('now')->format('c');

$c->options()->setOptions(
    injection: true,
    methodAttributes: true,
    propertyAttributes: true
);

$c->definitions()->bind('a', A::class, tags: ['service']);
$c->definitions()->bind('b', B::class, tags: ['service']);

foreach ($c->findByTag('service') as $svc) {
    $svc->handle();
}

use Infocyph\InterMix\DI\Container;
use Infocyph\InterMix\DI\Support\LifetimeEnum;

$c->bindFactory(
    Database::class,
    static fn (Container $container): Database => new Database(
        $container->get(DatabaseConfig::class),
    ),
    LifetimeEnum::Singleton,
    tags: ['infrastructure'],
);

// Equivalent fluent lifetime selection:
$c->factory(
    RequestContext::class,
    static fn (Container $container): RequestContext => new RequestContext(
        $container->get('request.id'),
    ),
)->scoped();

MacroTest::mix(new class {
    public function hello($name) {
        return "Hey, $name!";
    }
});

echo (new MacroTest)->hello('Ali'); // Hey, Ali!

use Infocyph\CacheLayer\Cache\Cache;

$pool = Cache::memory('intermix.definitions'); // or any PSR-6 pool
$c->definitions()->enableDefinitionCache($pool, generation: 'deployment-2026-08');
$report = $c->definitions()->warmDefinitionCache();

use Infocyph\InterMix\DI\Support\FactoryDefinition;
use Infocyph\InterMix\DI\Support\ServiceReference;

$path = __DIR__ . '/var/intermix.compiled.php';

// Explicit recipes can be executed dynamically and compiled safely.
$c->bind('mailer', FactoryDefinition::construct(Mailer::class, [
    new ServiceReference(Logger::class),
    'transactional',
]));

// Build time, after every definition and option is registered.
$c->compileTo($path);
$report = $c->compilationReport();

// Runtime, after performing the same registration.
$c->useCompiled($path);

use Infocyph\InterMix\Serializer\ClosureSerializer;

$payload = ClosureSerializer::serialize(static fn (int $value): int => $value * 2);
$closure = ClosureSerializer::unserialize($payload);

$signed = ClosureSerializer::signed($_ENV['APP_KEY']);
$signedPayload = $signed->serialize(static fn (): string => 'queued work');
$signedClosure = $signed->unserialize($signedPayload);