DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

Configure Psysh for magento 2.x.

Whilst I was developing in Laravel I used the tinker command:

php artisan tinker
Enter fullscreen mode Exit fullscreen mode

And I wanted this functionality to be in my magento installation. In order to achieve that in my project I made the following files:

./psysh_bootstrap.php

$file = getcwd() . '/app/bootstrap.php';

if(!file_exists($file)){
    return;
}

include_once  $file;
stream_wrapper_restore('phar');

if(class_exists(\Magento\Framework\App\Bootstrap::class)){
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();

    return $objectManager;
}

Enter fullscreen mode Exit fullscreen mode

Then upon .psysh.php I placed:

return [
    'commands' => [
        new \Psy\Command\ParseCommand,
    ],
    'defaultIncludes' => [
        __DIR__.'/psysh_bootstrap.php'
    ],
    'startupMessage' => "<info>Intecative shell with bootstrap</info>\n<fg=yellow;options=bold>\$objectManager</> contains magento's object manager",
];
Enter fullscreen mode Exit fullscreen mode

But what If I want theese settings globally (in docker specifically)?

There are cases that I did not was to mess project's ./vendor therefore I opted for a global installation.

At the Dockerfile I installed composer and psysh globally:

FROM composer as composer
FROM php

RUN mkdir -p /home/www-data/public_html &&\
    curl -sSLf \
        -o /usr/local/bin/install-php-extensions \
        https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
    chmod +x /usr/local/bin/install-php-extensions


#Install composer
COPY --from=composer /usr/bin/composer /bin/composer
RUN install-php-extensions zip &&\
    chown root:root /bin/composer &&\
    chmod +x /bin/composer &&\
    DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y git &&\
    apt-get autoremove && apt-get autoclean &&\
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* cache/* /var/lib/log/*

# Install psysh
RUN php -r "copy('https://psysh.org/psysh','/bin/psysh');" &&\
    chmod +x /bin/psysh &&\
    mkdir -p /var/www/.config/psysh &&\
    chown -R www-data:www-data /var/www/.config &&\
    chmod -R 666 /var/www/.config

VOLUME /var/www/.config/psysh


# Entrypoint configuration
Enter fullscreen mode Exit fullscreen mode

And my minimal entrypoint I could use:

#!/bin/sh

WEB_USER="www-data"

# rest of settings go here

mkdir -p /var/www/.config/psysh

cat > /var/www/.config/psysh/config.php<< EOF
<?php
return [
    'commands' => [
        new \Psy\Command\ParseCommand,
    ],
    'defaultIncludes' => [
        __DIR__.'/psysh_bootstrap.php'
    ],
    'startupMessage' => "<info>Intecative shell with bootstrap</info>\n<fg=yellow;options=bold>\\\$objectManager</> contains magento's object manager",
];
EOF

cat > /var/www/.config/psysh/psysh_bootstrap.php<< EOF
<?php

\$file = getcwd() . '/app/bootstrap.php';

if(!file_exists(\$file)){
    return;
}

include_once  \$file;
stream_wrapper_restore('phar');

if(class_exists(\Magento\Framework\App\Bootstrap::class)){
    \$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, \$_SERVER);
    \$objectManager = \$bootstrap->getObjectManager();

    return \$objectManager;
}
EOF

chown -R ${WEB_USER}:${WEB_USER} /var/www/.config
chown -R ${WEB_USER}:${WEB_USER} /var/www/.config/psysh

# I know I could use 666 but permissions not set correctly if mounted as volume
chmod -R 777 /var/www/.config/psysh
chmod -R 777 /var/www/.config
Enter fullscreen mode Exit fullscreen mode

There fore I could use:

$ psysh
Psy Shell v0.11.22 (PHP 7.4.33 — cli) by Justin Hileman
Intecative shell with bootstrap
$objectManager contains magento's object manager
> $objectManager->get(\Magento\Catalog\Model\Product::class);
= MageGuide\Bundler\Model\Product\Interceptor {#5897}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)