Whilst I was developing in Laravel I used the tinker command:
php artisan tinker
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;
}
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",
];
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
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
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}