skar / laminas-doctrine-orm

Simple Laminas/Mezzio Doctrine ORM integration
BSD 3-Clause "New" or "Revised" License
12 stars 5 forks source link

Package not working as expected. #6

Closed scruffycoderofficial closed 2 years ago

scruffycoderofficial commented 2 years ago

Hi,

I am running the following versions:

PHP

8.0

Mezzio

3.5.0

I have a MySQL service running behind docker that connects to my PHP container. These containers can talk to one another. The issue I am having, with the instructions given is that I am not able to get hold of the EntityManager within my ActionHandler classes. Also running vendor/bin/mezzio nor vendor/bin/laminas is yielding to any desired output.

Ideally, I would like to be able to have some of the commands from DoctrineORM at my disposal.

Steps (post package install)

Once this is done, supposedly, I am able to view DoctrineORM commands on either Mezzio command-line and/or laminas-cli.

Classes involved #5

Action class

declare(strict_types=1);

namespace Capable\Platform\App\Action\Handler;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\JsonResponse;
use Capable\Platform\App\Action\Concern\ContainerAwareTrait;
use Capable\Platform\App\Action\Contract\ContainerAwareInterface;

/**
 * Class PingHandler
 *
 * @package Capable\Platform\App\Action\Handler
 */
class PingHandler implements RequestHandlerInterface, ContainerAwareInterface
{
    use ContainerAwareTrait;

    /** {@inheritDoc} */
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $contacts = $this->getEntityManager()->getRepository('Contact')->findAll();

        return new JsonResponse([
            'data' => [$contacts],
            'success' => true
        ]);
    }
}

/*************************************************************************************/

declare(strict_types=1);

namespace Capable\Platform\App\Action\Contract;

/**
 * Interface ContainerAwareInterface
 *
 * @package Capable\Platform\App\Action\Contract
 */
interface ContainerAwareInterface{}

declare(strict_types=1);

/*************************************************************************************/

namespace Capable\Platform\App\Action\Concern;

use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;

trait ContainerAwareTrait
{
    private $sm;

    public function __construct(ContainerInterface $sm)
    {
        $this->sm = $sm;
    }

    protected function getServiceManager()
    {
        return $this->sm;
    }

    protected function getEntityManager()
    {
        return $this->getServiceManager()->get(EntityManager::class);
    }
}

/*************************************************************************************/

declare(strict_types=1);

namespace Capable\Platform\App\Action\Factory;

use Psr\Container\ContainerInterface;
use Capable\Platform\App\Action\Contract\ContainerAwareInterface;

/**
 * Class HandlerFactory
 *
 * @package Capable\Platform\App\Action\Factory
 */
final class ActionHandlerFactory
{
    public function __invoke(ContainerInterface $container, string $name, callable $callback) : ContainerAwareInterface
    {
        return new $name($container);
    }
}

And lastly, below is my ConfigProvider class which contains the rest of the configurations.

use Mezzio\Container;
use Mezzio\Middleware;
use Psr\Container\ContainerInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Capable\Platform\App\Action\Handler\PingHandler;
use Capable\Platform\App\Action\Factory\ActionHandlerFactory;
use Laminas\Stratigility\Middleware\ErrorHandler;

/**
 * The configuration provider for the App module
 *
 * @see https://docs.zendframework.com/zend-component-installer/
 */
class ConfigProvider
{
    /**
     * Returns the configuration array
     *
     * To add a bit of a structure, each section is defined in a separate
     * method which returns an array with its configuration.
     *
     */
    public function __invoke(): array
    {
        return [
            'dependencies' => $this->getDependencies(),
        ];
    }

    /**
     * Returns the container dependencies
     */
    public function getDependencies(): array
    {
        return [
            'delegators' => [
                PingHandler::class => [ ActionHandlerFactory::class, ],
            ],
            'invokables' => [
                PingHandler::class => PingHandler::class,
            ],

            'factories' => [
                'doctrine.cache.array' => function(ContainerInterface $container) {
                    return new ArrayAdapter();
                },
                ErrorHandler::class => Container\ErrorHandlerFactory::class,
                Middleware\ErrorResponseGenerator::class => Container\ErrorResponseGeneratorFactory::class,
            ],
        ];
    }
}

Bonus

A partial version of composer.json file.

"require": {
        "php": "^8.0",
        "composer/package-versions-deprecated": "^1.10.99",
        "illuminate/support": "^9.7",
        "laminas/laminas-component-installer": "^2.5.0",
        "laminas/laminas-config-aggregator": "^1.5.0",
        "laminas/laminas-diactoros": "^2.6.0",
        "laminas/laminas-servicemanager": "^3.4",
        "laminas/laminas-stdlib": "^3.3.1",
        "laminas/laminas-zendframework-bridge": "^1.2.0",
        "league/oauth2-server": "^8.3",
        "mezzio/mezzio": "^3.5.0",
        "mezzio/mezzio-authentication-oauth2": "^2.1",
        "mezzio/mezzio-fastroute": "^3.0.3",
        "mezzio/mezzio-helpers": "^5.6.0",
        "psr/http-factory": "^1.0",
        "psr/http-message": "^1.0",
        "psr/http-server-middleware": "^1.0",
        "skar/laminas-doctrine-orm": "^0.1.0",
        "symfony/cache": "^6.0",
        "symfony/dotenv": "^6.0",
        "webmozart/assert": "^1.10"
    },
    "require-dev": {
        "filp/whoops": "^2.7.1",
        "helmich/phpunit-psr7-assert": "^4.3",
        "laminas/laminas-development-mode": "^3.3.0",
        "mezzio/mezzio-tooling": "^1.4.0",
        "phpspec/prophecy-phpunit": "^2.0",
        "phpunit/php-code-coverage": "^9.2",
        "phpunit/phpunit": "^9.5",
        "psalm/plugin-phpunit": "^0.16.1",
        "roave/security-advisories": "dev-master",
        "squizlabs/php_codesniffer": "^3.6",
        "weirdan/doctrine-psalm-plugin": "^1.1"
    },

I am trying to figure out what it is that I am perhaps not doing right.

Could you please assist?

skar commented 2 years ago

If I understand you correctly...

  1. Check config/config.php for defined \Skar\LaminasDoctrineORM\ConfigProvider::class, in config providers list
  2. Try to use the config suggested in the README.md file (and don't forgot change host, you should use the database container hostname)
  3. if you are running the application in a container you should run CLI commands in that container (e.g. docker exec my-image-name vendor/bin/laminas)
  4. To get EntityManager from the container: $container->get(\Doctrine\ORM\EntityManager::class);
  5. Try to use "dev-master" version. I will bump version later.
scruffycoderofficial commented 2 years ago

@skar thank you for your help. The problem was that the module installer did not add the ConfigProvider within the coonfig.php file. Maybe we can add a note in the README to highlight this otherwise great work and thank you for your support. A very elegant and easy-to-use library.

I am not that well experienced with Zend Framework but I guess we could also, in the future, provide means for users to either choose to use Doctrine Cache or Symfony implementation or any other.

I think this would go a long way really. Thanks again.

^ Doctrine\ORM\EntityManager {[#114 ▼](http://0.0.0.0:8080/api/ping#sf-dump-162183931-ref2114)
  -config: Doctrine\ORM\Configuration {[#116 ▶](http://0.0.0.0:8080/api/ping#sf-dump-162183931-ref2116)}
  -conn: Doctrine\DBAL\Connection {[#132 ▶](http://0.0.0.0:8080/api/ping#sf-dump-162183931-ref2132)}
  -metadataFactory: Doctrine\ORM\Mapping\ClassMetadataFactory {[#136 ▶](http://0.0.0.0:8080/api/ping#sf-dump-162183931-ref2136)}
  -unitOfWork: Doctrine\ORM\UnitOfWork {[#138 ▶](http://0.0.0.0:8080/api/ping#sf-dump-162183931-ref2138)}
  -eventManager: Doctrine\Common\EventManager {[#115 ▶](http://0.0.0.0:8080/api/ping#sf-dump-162183931-ref2115)}
  -proxyFactory: Doctrine\ORM\Proxy\ProxyFactory {[#145 ▶]()}
  -repositoryFactory: Doctrine\ORM\Repository\DefaultRepositoryFactory {[#137 ▶]()}
  -expressionBuilder: null
  -closed: false
  -filterCollection: null
  -cache: null
}
skar commented 2 years ago

They was removed Cache from the Doctrine library to force using a PSR compatible cache libraries. You can use any other compatible library. I've use the Skar\Cache as fast replace and working out of the box.

Later I will update scripts to use installer.

Thank you for using my library. Feel free to contact me.