PHP code example of hartmann / planck

1. Go to this page and download the library: Download hartmann/planck 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/ */

    

hartmann / planck example snippets


$container = new \Hartmann\Planck\Container();

use Interop\Container\ServiceProviderInterface

class Provider implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            stdClass::class => function(ContainerInterface $container) {
                return new stdClass;
            },
            ...
        ];
    }
    
    public function getExtensions()
    {
        return [
            stdClass::class => function(ContainerInterface $container, ?stdClass $class) {
                $class->foo = 'bar';
                
                return $class;
            },
            ...
        ];
    }
}

$class = $container->get(stdClass::class);

$container->set('factory', $container->factory(function (ContainerInterface $container) {
    return new stdClass;
}));

$container->set('cookie_name', 'SESSION_ID');
$container->set('session_storage_class', 'SessionStorage');

$container->set('random_bytes', $container->preserve(function () {
    return random_bytes(4);
}));

$container->set('session_storage', function (ContainerInterface $container) {
    return new $container->get('session_storage_class')($container->get('cookie_name'));
});

$container->extend('session_storage', function (ContainerInterface $container, ?SessionStorage $storage) {
    $storage->...();

    return $storage;
});

$container->set(Foo::class, new Foo());
$container->set(Bar::class, new Bar());

$container->set('autowired', $container->autowire(function (Foo $foo, Bar $bar) {
    return ...
}));

class Foo {
    ...
}

$container->set(Foo::class, new Foo());
$container->set('autowired', $container->autowire(function (Foo $foo, $bar) {
    var_dump($foo) // object Foo
    var_dump($bar) // string 'foo'
}, ['bar' => 'foo']]));

class HomeController {
    
    protected $logger
    
    public function __contruct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
    
    public function index(Request $request, Response $response): Response 
    {
        $this->logger->info('someone visited my site!');
        
        return $response->write('Hello');
    }    
}

// adding the 

$container->autowire([$homeControllerInstance, 'index']);

class Request
{
    public function __construct(string $method, UriInterface $uri, HeadersInterface $headers, ...);
}

class CreateUserRequest extends Request {
    ... 
}

// adding the 

// unresolvable, must be passed directly to the parameters
function ($foo) {  
}                             

// unresolvable, must be passed directly to the parameters
function (string|int|float|array|bool $foo) {  
}

// hinted, 

$container->enableImplicitAutowiring(true); // enable
$container->enableImplicitAutowiring(false); // disable

$container = new \Hartmann\Planck\Container
$container->enableImplicitAutowiring(true);

$container->set('autowired', $container->autowire(function(Foo $foo, Bar $bar) {
    return ...
}));

$value = $container->get('autowired');

use \Hartmann\ResolveStrategy\ResolveStrategyInterface

class RequestResolveStrategy implements ResolveStrategyInterface
{
    public function suitable(string $class): bool
    {
        return method_exists($class, 'createFromEnvironment') && in_array(FormRequest::class, class_parents($class));
    }

    public function resolve(\Psr\Container\ContainerInterface $container, string $class)
    {
        return call_user_func([$class, 'createFromEnvironment'], $container->get('environment'));
    }
}

$container = new \Hartmann\Planck\Container();

$container->enableImplicitAutowiring(true);
$container->addResolveStrategy(new RequestResolveStrategy());

$container->get(CreateUserFormRequest::class);
$container->get(DeletePostFormRequest::class);
$container->get(LoginFormRequest::class);