PHP code example of pine3ree / pine3ree-auto-resolving-factory
1. Go to this page and download the library: Download pine3ree/pine3ree-auto-resolving-factory 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/ */
pine3ree / pine3ree-auto-resolving-factory example snippets
// file: src/My/App/Model/PostMapper.php
use My\App\Database\ConnectionInterface;
use My\App\Database\HydratorInterface;
//...
class PostMapper
{
ConnectionInterface $db;
HydratorInterface $hydrator;
public function __construct(ConnectionInterface $db, HydratorInterface $hydrator)
{
$this->db = $db;
$this->hydrator = $hydrator;
}
//...
}
//------------------------------------------------------------------------------
// file: test.php
use My\App\Model\PostMapper;
use Psr\Container\ContainerInterface;
use pine3ree\Container\Factory\AutoResolveFactory;
$container =
$postMapper = $container->get(PostMapper::class);
// file: config/dependencies.php
//...
use My\App\Database\ConnectionInterface;
use My\App\Database\PdoConnection;
use My\App\Database\PdoConnectionFactory;
use My\App\Database\HydratorInterface;
use My\App\Model\PostMapper;
use pine3ree\Container\Factory\AutoResolveFactory;
//...
return [
//...
'invokables' => [
//...
HydratorInterface::class => HydratorInterface::class,
//...
],
'aliases' => [
//...
ConnectionInterface::class => PdoConnection::class,
//...
],
'factories' => [
//...
PdoConnection::class => PdoConnectionFactory::class,
PostMapper::class => AutoResolveFactory::class, // *
PostListHandler::class => AutoResolveFactory::class, // *
//...
],
//...
];
// file: my/App/Handler/PostListHandler.php ------------------------------------
use My\App\Model\PostMapper;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class PostListHandler implements RequestHandlerInterface
{
public function __construct(PostMapper $postMapper): ResponseInterface
{
$this->postMapper = $postMapper;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
// use $this->postMapper and $request to build and return a response
}
}