Search code examples
phpsymfonydockerrabbitmqamqp

Cannot load PHP class when running Symfony command


I would appreciate any help I can get with following issue.

I've set up Symfony Messenger to use AMQP and RabbitMQ. I can dispatch message and make use of AMQPConnection within my project.

But when I try to consume the message I get following:

    $   php bin/console messenger:consume-messages amqp

        Attempted to load class "AMQPConnection" from the global namespace.
        Did you forget a "use" statement for "PhpAmqpLib\Connection\AMQPConnection"?

The console command calls a method within AmqpFactory.php

    public function createConnection(array $credentials): \AMQPConnection
    {
        return new \AMQPConnection($credentials);
    }

So when I run the project from the browser, everything works fine. I can use AMQPConnection and all. But when running from terminal, it cannot find the AMQPConnection class.

From within my composer.json I also have the amqplib installed

    "php-amqplib/php-amqplib": "^2.7",

I use Docker and the Dockerfile contains:

    FROM php:7-apache

    RUN usermod -u 1000 www-data &&\
        a2dissite 000-default &&\
        apt-get update &&\
        apt-get install -y \
            zlib1g-dev \
            libicu-dev \
            g++ \
            libfreetype6-dev \
            libssh-dev \
            libjpeg62-turbo-dev \
            libmcrypt-dev \
            librabbitmq-dev \
            libpng-dev &&\
        docker-php-ext-configure intl &&\
        docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ &&\
        docker-php-ext-install -j$(nproc) gd pdo_mysql intl opcache zip pcntl exif bcmath sockets &&\
        a2enmod rewrite

    RUN pecl install amqp \
        && docker-php-ext-enable amqp

    COPY docker/apache2.conf /etc/apache2/apache2.conf

    WORKDIR /var/www/project

UPDATE! I've printed the phpinfo() and when dispatching the message I have version 7.2.8 and when executing the terminal command I have 7.2.7. So terminal is not using the Docker instance of PHP when executing the command.


Solution

  • Solved this, finally. I had no idea I had to go via Docker to run this command. This worked for me:

    docker exec -ti container_name sh -c "cd /var/www/project/ && php bin/console messenger:consume-messages amqp"