1. Go to this page and download the library: Download woohoolabs/zen 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/ */
woohoolabs / zen example snippets
/**
* @param B $b
*/
public function __construct(A $a, $b, $c = true)
{
// ...
}
#[Inject]
/** @var A */
private $a;
#[Inject]
private B $b;
$container = new Container();
$builder = new FileSystemContainerBuilder(new CompilerConfig(), "/var/www/src/Container/CompiledContainer.php");
$builder->build();
class CompilerConfig extends AbstractCompilerConfig
{
public function getContainerNamespace(): string
{
return "App\\Container";
}
public function getContainerClassName(): string
{
return "Container";
}
public function useConstructorInjection(): bool
{
return true;
}
public function usePropertyInjection(): bool
{
return true;
}
public function getContainerConfigs(): array
{
return [
new ContainerConfig(),
];
}
}
class ContainerConfig extends AbstractContainerConfig
{
protected function getEntryPoints(): array
{
return [
// Define all classes in a PSR-4 namespace as Entry Points
Psr4NamespaceEntryPoint::singleton('WoohooLabs\Zen\Examples\Controller'),
// Define all classes in a directory as Entry Points
WildcardEntryPoint::singleton(__DIR__ . "/Controller"),
// Define a class as Entry Point
ClassEntryPoint::singleton(UserController::class),
];
}
protected function getDefinitionHints(): array
{
return [
// Bind the Container class to the ContainerInterface (Singleton scope by default)
ContainerInterface::class => Container::class,
// Bind the Request class to the RequestInterface (Prototype scope)
RequestInterface::class => DefinitionHint::prototype(Request::class),
// Bind the Response class to the ResponseInterface (Singleton scope)
ResponseInterface::class => DefinitionHint::singleton(Response::class),
];
}
protected function getWildcardHints(): array
{
return [
// Bind all classes in the specified PSR-4 namespaces to each other based on patterns
new Psr4WildcardHint(
'WoohooLabs\Zen\Examples\Domain\*RepositoryInterface',
'WoohooLabs\Zen\Examples\Infrastructure\Mysql*Repository'
),
// Bind all classes in the specified directories to each other based on patterns
new WildcardHint(
__DIR__ . "/Domain",
'WoohooLabs\Zen\Examples\Domain\*RepositoryInterface',
'WoohooLabs\Zen\Examples\Infrastructure\Mysql*Repository'
),
];
}
}
protected function getEntryPoints(): array
{
return [
new WildcardEntryPoint(__DIR__ . "/Controller"),
];
}
protected function getEntryPoints(): array
{
return [
new Psr4NamespaceEntryPoint('Src\Controller'),
];
}
protected function getEntryPoints(): array
{
return [
new ClassEntryPoint(UserController::class),
];
}
class NewRelicHandler implements LoggerInterface {}
class PhpConsoleHandler implements LoggerInterface {}
class MailHandler implements LoggerInterface {}
class ServiceA
{
public function __construct(LoggerInterface $logger) {}
}
class ServiceB
{
public function __construct(LoggerInterface $logger) {}
}
class ServiceC
{
public function __construct(LoggerInterface $logger) {}
}
public function getFileBasedDefinitionConfig(): FileBasedDefinitionConfigInterface
{
return FileBasedDefinitionConfig::enableGlobally("Definitions");
}