Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ process exits with that status.
The client detects the project type automatically:

- Laravel: `vendor/autoload.php` and `bootstrap/app.php`
- Magento: `bin/magento`
- Symfony: `vendor/autoload.php`, `symfony.lock`, and `src/Kernel.php`
- WordPress: `wp-load.php`
- Pimcore: `vendor/pimcore/pimcore`
Expand All @@ -147,6 +148,10 @@ Examples:
code=$(printf '%s' 'return App\Models\User::query()->latest()->first();' | base64)
php client.phar /path/to/laravel execute "$code"

# Magento
code=$(printf '%s' 'echo Magento\Framework\App\ObjectManager::getInstance()->get(Magento\Catalog\Model\ProductRepository::class)->getById(1)->getName();' | base64)
php client.phar /path/to/magento execute "$code"

# WordPress
code=$(printf '%s' 'return get_option("blogname");' | base64)
php client.phar /path/to/wordpress execute "$code"
Expand Down
5 changes: 5 additions & 0 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use TweakPHP\Client\Loaders\ComposerLoader;
use TweakPHP\Client\Loaders\LaravelLoader;
use TweakPHP\Client\Loaders\LoaderInterface;
use TweakPHP\Client\Loaders\MagentoLoader;
use TweakPHP\Client\Loaders\PimcoreLoader;
use TweakPHP\Client\Loaders\PlainPhpLoader;
use TweakPHP\Client\Loaders\SymfonyLoader;
Expand All @@ -29,6 +30,10 @@ public static function load(string $path, ?string $encodedLoader = null)
return new LaravelLoader($path);
}

if (MagentoLoader::supports($path)) {
return new MagentoLoader($path);
}

if (SymfonyLoader::supports($path)) {
return new SymfonyLoader($path);
}
Expand Down
40 changes: 40 additions & 0 deletions src/Loaders/MagentoLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace TweakPHP\Client\Loaders;

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ProductMetadataInterface;

class MagentoLoader extends ComposerLoader
{
public static function supports(string $path): bool
{
return file_exists($path.'/bin/magento');
}

public function __construct(string $path)
{
parent::__construct($path);

require $path.'/app/bootstrap.php';
if (! in_array('phar', stream_get_wrappers())) {
stream_wrapper_restore('phar');
}

Bootstrap::create($path, $_SERVER);
}

public function name(): string
{
return 'Magento';
}

public function version(): string
{
$objectManager = ObjectManager::getInstance();
$metadata = $objectManager->get(ProductMetadataInterface::class);

return $metadata->getEdition().' '.$metadata->getVersion();
}
}
33 changes: 33 additions & 0 deletions tests/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function kernel() {}
use TweakPHP\Client\Loader;
use TweakPHP\Client\Loaders\ComposerLoader;
use TweakPHP\Client\Loaders\LaravelLoader;
use TweakPHP\Client\Loaders\MagentoLoader;
use TweakPHP\Client\Loaders\PimcoreLoader;
use TweakPHP\Client\Loaders\PlainPhpLoader;
use TweakPHP\Client\Loaders\SymfonyLoader;
Expand Down Expand Up @@ -101,6 +102,38 @@ public function basePath() {
$this->assertArrayHasKey('Illuminate\Support\Collection', $casters);
}

public function test_magento_loader_detection_and_boot()
{
mkdir($this->tempDir.'/app', 0777, true);
mkdir($this->tempDir.'/bin', 0777, true);
mkdir($this->tempDir.'/vendor', 0777, true);
touch($this->tempDir.'/bin/magento');
file_put_contents($this->tempDir.'/vendor/autoload.php', '<?php ');

$bootstrapMock = '<?php
namespace Magento\Framework\App {
class Bootstrap {
public static function create() {}
}
class ObjectManager {
public static function getInstance() { return new static; }
public function get() { return new ProductMetadata; }
}
class ProductMetadata {
public function getEdition() { return "Magento Community"; }
public function getVersion() { return "2.4.9"; }
}
}';
file_put_contents($this->tempDir.'/app/bootstrap.php', $bootstrapMock);

$this->assertTrue(MagentoLoader::supports($this->tempDir));

$loader = Loader::load($this->tempDir);
$this->assertInstanceOf(MagentoLoader::class, $loader);
$this->assertEquals('Magento', $loader->name());
$this->assertEquals('Magento Community 2.4.9', $loader->version());
}

public function test_symfony_loader_detection_and_boot()
{
mkdir($this->tempDir.'/vendor', 0777, true);
Expand Down