Skip to content

Commit

Permalink
Merge pull request #415 from modmore/fix-modx3-package-install
Browse files Browse the repository at this point in the history
Fix installing packages on MODX 3.x
  • Loading branch information
muzzwood authored Dec 15, 2022
2 parents b01d335 + 69fc3fa commit 6f5faa4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
16 changes: 16 additions & 0 deletions application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
exit(1);
}

/**
* Preload Gitify dependencies, to avoid potential conflicts with MODX 3.x core dependencies.
*/
$classes = [
\Symfony\Component\Console\Input\InputArgument::class,
\Symfony\Component\Console\Input\InputInterface::class,
\Symfony\Component\Console\Input\InputOption::class,
\Symfony\Component\Console\Output\OutputInterface::class,
\Symfony\Component\Console\Helper\QuestionHelper::class,
\Symfony\Component\Console\Question\ChoiceQuestion::class,
\Symfony\Component\Console\Question\ConfirmationQuestion::class,
];
foreach ($classes as $className) {
$loaded = class_exists($className);
}

/**
* Ensure the timezone is set; otherwise you'll get a shit ton (that's a technical term) of errors.
*/
Expand Down
31 changes: 31 additions & 0 deletions src/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
namespace modmore\Gitify;

use modX;
use MODX\Revolution\modCategory;
use MODX\Revolution\modContext;
use MODX\Revolution\modDashboardWidget;
use MODX\Revolution\modElement;
use MODX\Revolution\modStaticResource;
use MODX\Revolution\modTemplateVar;
use MODX\Revolution\Transport\modTransportPackage;
use MODX\Revolution\Transport\modTransportProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -27,6 +35,7 @@ abstract class BaseCommand extends Command
public $loadConfig = true;
public $loadMODX = true;
public $isUpgrade = false;
public $isMODX3 = false;

/**
* Initializes the command just after the input has been validated.
Expand All @@ -50,6 +59,28 @@ public function initialize(InputInterface $input, OutputInterface $output)
if ($this->loadMODX)
{
$this->modx = Gitify::loadMODX();

$modxVersion = $this->modx->getVersionData();
if (version_compare($modxVersion['full_version'], '3.0.0-dev', '>=')) {
$this->isMODX3 = true;
}

// If we're on MODX 3, set up some class aliases.
if ($this->isMODX3) {
class_alias(modTransportProvider::class, 'modTransportProvider');
class_alias(modTransportPackage::class, 'modTransportPackage');
class_alias(modContext::class, 'modContext');
class_alias(modElement::class, 'modElement');
class_alias(modStaticResource::class, 'modStaticResource');
class_alias(modDashboardWidget::class, 'modDashboardWidget');
class_alias(modTemplateVar::class, 'modTemplateVar');
class_alias(modCategory::class, 'modCategory');

// Avoid warnings in xPDO 3.x if $_SESSION isn't available.
if (!isset($_SESSION)) {
session_start();
}
}
}
}

Expand Down
35 changes: 20 additions & 15 deletions src/Command/InstallPackageCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace modmore\Gitify\Command;

use GuzzleHttp\Psr7\Response;
use modmore\Gitify\Gitify;
use modmore\Gitify\BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -214,7 +215,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
private function install($package, $provider = 0, array $installOptions = [])
{
$this->modx->addPackage('modx.transport', MODX_CORE_PATH . 'model/');
if (!$this->isMODX3) {
$this->modx->addPackage('modx.transport', MODX_CORE_PATH . 'model/');
}

if (!($provider instanceof \modTransportProvider) && is_numeric($provider) && $provider > 0)
{
Expand Down Expand Up @@ -286,9 +289,16 @@ private function download($packageName, $provider, $options = []) {
]);

// When we got a match (non 404), extract package information
if (!$response->isError()) {
if ($this->isMODX3) {
$error = $response->getStatusCode() !== 200;
}
else {
$error = $response->isError();
}

$foundPkg = simplexml_load_string($response->response);
if (!$error) {
$responseBody = $response instanceof Response ? $response->getBody()->getContents() : $response->response;
$foundPkg = simplexml_load_string($responseBody);

// Verify that signature matches (mismatches are known to occur!)
if ($foundPkg->signature == $packageName) {
Expand All @@ -312,7 +322,8 @@ private function download($packageName, $provider, $options = []) {
]);

if (!empty($response)) {
$foundPackages = simplexml_load_string($response->response);
$responseBody = $response instanceof Response ? $response->getBody()->getContents() : $response->response;
$foundPackages = simplexml_load_string($responseBody);

foreach ($foundPackages as $foundPkg) {
// Only accept exact match on signature
Expand All @@ -338,8 +349,8 @@ private function download($packageName, $provider, $options = []) {

// Check for a proper response
if (!empty($response)) {

$foundPackages = simplexml_load_string($response->response);
$responseBody = $response instanceof Response ? $response->getBody()->getContents() : $response->response;
$foundPackages = simplexml_load_string($responseBody);

// No matches, simply return
if ($foundPackages['total'] == 0) {
Expand Down Expand Up @@ -447,15 +458,9 @@ private function download($packageName, $provider, $options = []) {
}

if ($this->interactive && !$selectedFromMultiVersions) {
if (!$helper->ask(
$this->input,
$this->output,
new ConfirmationQuestion(
"Do you want to install <info>{$package['name']} ({$package['version']})</info>? <comment>[Y/n]</comment>: ",
true
)
)
) {
$question = new ConfirmationQuestion(
"Do you want to install <info>{$package['name']} ({$package['version']})</info>? <comment>[Y/n]</comment>: ", true);
if (!$helper->ask($this->input, $this->output, $question)) {
continue;
}
}
Expand Down

0 comments on commit 6f5faa4

Please sign in to comment.