Skip to content

Commit

Permalink
refactored final
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Jan 6, 2025
1 parent c7452f3 commit 21cf8ee
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 59 deletions.
31 changes: 16 additions & 15 deletions src/OpenMage/ComposerPlugin/Copy/AbstractCopyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use Composer\Package\BasePackage;
use Composer\Script\Event;
use Exception;
use OpenMage\ComposerPlugin\Copy\Composer as Composer;
use OpenMage\ComposerPlugin\Copy\Npm as Npm;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -68,7 +66,7 @@ public function __construct(Event $event)
public function processComposerInstall(): void
{
$copySourcePath = null;
if ($this instanceof Composer\PluginInterface) {
if ($this instanceof CopyFromComposerInterface) {
$package = $this->getComposerPackage();
if (!$package) {
return;
Expand All @@ -78,15 +76,15 @@ public function processComposerInstall(): void

$filesystem = new Filesystem();

if ($this instanceof Npm\PluginInterface && (!$copySourcePath || !$filesystem->exists($copySourcePath))) {
if ($this instanceof CopyFromNpmInterface && (!$copySourcePath || !$filesystem->exists($copySourcePath))) {
if ($this->event->getIO()->isVerbose()) {
$this->event->getIO()->write(sprintf('Fallback to NPM for %s', $this->getNpmPackageName()));
}
$this->processNpmInstall();
return;
}

if ($copySourcePath && $this instanceof Composer\PluginInterface) {
if ($copySourcePath && $this instanceof CopyFromComposerInterface) {
$finder = Finder::create()
->files()
->in($copySourcePath)
Expand All @@ -113,8 +111,11 @@ public function processComposerInstall(): void
*/
public function processNpmInstall(): void
{
if ($this instanceof Npm\PluginInterface) {
$filesystem = new Filesystem();
if ($this instanceof CopyFromNpmInterface) {
if (!$this->getVersion()) {
return;
}

$sourcePath = $this->getNpmFilePath();

if ($this->event->getIO()->isVerbose()) {
Expand All @@ -134,14 +135,14 @@ public function processNpmInstall(): void
$this->event->getIO()->write($errorException->getMessage());
return;
}
$content = file_get_contents($sourceFilePath);

if (!$content) {
$this->event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
return;
}

try {
$filesystem = new Filesystem();
$targetFilePath = $this->getCopyTargetPath() . '/' . $fileName;
$filesystem->dumpFile($targetFilePath, $content);
if ($this->event->getIO()->isVerbose()) {
Expand All @@ -157,7 +158,7 @@ public function processNpmInstall(): void

public function getComposerPackage(): ?BasePackage
{
if ($this instanceof Composer\PluginInterface) {
if ($this instanceof CopyFromComposerInterface) {
$vendorName = $this->getComposerPackageName();
$module = $this->getInstalledComposerPackage($vendorName);
if ($module) {
Expand Down Expand Up @@ -185,7 +186,7 @@ public function getComposerPackage(): ?BasePackage
*/
public function getNpmPackage(): ?array
{
if ($this instanceof Npm\PluginInterface) {
if ($this instanceof CopyFromNpmInterface) {
$vendorName = $this->getNpmPackageName();

$locker = $this->event->getComposer()->getLocker();
Expand Down Expand Up @@ -229,10 +230,10 @@ public function getNpmPackage(): ?array
*/
protected function getNpmFilePath(): string
{
if ($this instanceof Npm\PluginInterface) {
if ($this instanceof CopyFromNpmInterface) {
$search = ['{{package}}', '{{version}}'];
$replace = [$this->getNpmPackageName(), $this->getVersion()];
return str_replace($search, $replace, Npm\PluginInterface::NPM_FALLBACK_URL);
return str_replace($search, $replace, CopyFromNpmInterface::NPM_FALLBACK_URL);
}
return '';
}
Expand All @@ -245,11 +246,11 @@ private function getVersion(): string
if (is_null($this->version)) {
$version = '';
switch (true) {
case $this instanceof Composer\PluginInterface:
case $this instanceof CopyFromComposerInterface:
$package = $this->getComposerPackage();
$version = $package ? $package->getPrettyVersion() : '';
break;
case $this instanceof Npm\PluginInterface:
case $this instanceof CopyFromNpmInterface:
$package = $this->getNpmPackage();
$version = $package ? $package['version'] : '';
break;
Expand Down Expand Up @@ -301,7 +302,7 @@ protected function getMageRootDirectoryFromComposer(): string

protected function getCopySourcePath(): string
{
if ($this instanceof Composer\PluginInterface) {
if ($this instanceof CopyFromComposerInterface) {
return sprintf(
'%s/%s/%s',
$this->getVendorDirectoryFromComposer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Composer;
namespace OpenMage\ComposerPlugin\Copy;

/**
* PluginInterface
*/
interface PluginInterface
interface CopyFromComposerInterface
{
/**
* Npm name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Npm;
namespace OpenMage\ComposerPlugin\Copy;

/**
* PluginInterface
*/
interface PluginInterface
interface CopyFromNpmInterface
{
public const NPM_FALLBACK_URL = 'https://unpkg.com/{{package}}@{{version}}/dist/';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Composer\Plugins;
namespace OpenMage\ComposerPlugin\Copy\Plugins;

use OpenMage\ComposerPlugin\Copy;

/**
* Class JQuery
*/
class ChartJs extends Copy\AbstractCopyPlugin implements Copy\Composer\PluginInterface, Copy\Npm\PluginInterface
class ChartJs extends Copy\AbstractCopyPlugin implements Copy\CopyFromComposerInterface, Copy\CopyFromNpmInterface
{
public function getNpmPackageName(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Npm\Plugins;
namespace OpenMage\ComposerPlugin\Copy\Plugins;

use OpenMage\ComposerPlugin\Copy;

/**
* Class Flatpickr
*/
class Flatpickr extends Copy\AbstractCopyPlugin implements Copy\Npm\PluginInterface
class Flatpickr extends Copy\AbstractCopyPlugin implements Copy\CopyFromNpmInterface
{
public function getNpmPackageName(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Composer\Plugins;
namespace OpenMage\ComposerPlugin\Copy\Plugins;

use OpenMage\ComposerPlugin\Copy;

/**
* Class JQuery
*/
class JQuery extends Copy\AbstractCopyPlugin implements Copy\Composer\PluginInterface
class JQuery extends Copy\AbstractCopyPlugin implements Copy\CopyFromComposerInterface
{
public function getComposerPackageName(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Composer\Plugins;
namespace OpenMage\ComposerPlugin\Copy\Plugins;

use Composer\Package\BasePackage;
use OpenMage\ComposerPlugin\Copy;
Expand All @@ -25,7 +25,7 @@
/**
* Class TinyMce
*/
class TinyMce extends Copy\AbstractCopyPlugin implements Copy\Composer\PluginInterface
class TinyMce extends Copy\AbstractCopyPlugin implements Copy\CopyFromComposerInterface
{
public const TINYMCE_LICENSE_FILE = 'LICENSE_TINYMCE.txt';
public const TINYMCE_LICENSE_NOTE = 'LICENSE_TINYMCE_OPENMAGE.txt';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

declare(strict_types=1);

namespace OpenMage\ComposerPlugin\Copy\Composer\Plugins;
namespace OpenMage\ComposerPlugin\Copy\Plugins;

use Composer\InstalledVersions;
use OpenMage\ComposerPlugin\Copy;

/**
* Class TinyMceLanguages
*/
class TinyMceLanguages extends Copy\AbstractCopyPlugin implements Copy\Composer\PluginInterface
class TinyMceLanguages extends Copy\AbstractCopyPlugin implements Copy\CopyFromComposerInterface
{
public const TINYMCE = 'tinymce/tinymce';

Expand Down
45 changes: 15 additions & 30 deletions src/OpenMage/ComposerPlugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,41 @@ public function uninstall(Composer $composer, IOInterface $io): void {}

/**
* @see EventSubscriberInterface::getSubscribedEvents
* @see processCopyFromVendor
* @see processCopyFromNpm
* @see processCopy
*/
public static function getSubscribedEvents(): array
{
return [
ScriptEvents::POST_INSTALL_CMD => [
['processCopyFromVendor'],
['processCopyFromNpm'],
['processCopy'],
],
ScriptEvents::POST_UPDATE_CMD => [
['processCopyFromVendor'],
['processCopyFromNpm'],

['processCopy'],
],
];
}

/**
* @see \OpenMage\ComposerPlugin\Copy\Composer\Plugins\ChartJs
* @see \OpenMage\ComposerPlugin\Copy\Composer\Plugins\JQuery
* @see \OpenMage\ComposerPlugin\Copy\Composer\Plugins\TinyMce
* @see \OpenMage\ComposerPlugin\Copy\Composer\Plugins\TinyMceLanguages
* @see \OpenMage\ComposerPlugin\Copy\Plugins\ChartJs
* @see \OpenMage\ComposerPlugin\Copy\Plugins\Flatpickr
* @see \OpenMage\ComposerPlugin\Copy\Plugins\JQuery
* @see \OpenMage\ComposerPlugin\Copy\Plugins\TinyMce
* @see \OpenMage\ComposerPlugin\Copy\Plugins\TinyMceLanguages
*/
public function processCopyFromVendor(Event $event): void
public function processCopy(Event $event): void
{
$plugins = $this->getPlugins(__DIR__ . '/Copy/Composer/Plugins');
$plugins = $this->getPlugins(__DIR__ . '/Copy/Plugins');
foreach ($plugins as $plugin) {
$pluginLoaded = new $plugin($event);
if (!$pluginLoaded instanceof Copy\Composer\PluginInterface) {
$this->io->write('Could not load ' . $plugin);
if ($pluginLoaded instanceof Copy\CopyFromComposerInterface) {
$pluginLoaded->processComposerInstall();
continue;
}
$pluginLoaded->processComposerInstall();
}
}

/**
* @see \OpenMage\ComposerPlugin\Copy\Npm\Plugins\Flatpickr
*/
public function processCopyFromNpm(Event $event): void
{
$plugins = $this->getPlugins(__DIR__ . '/Copy/Npm/Plugins');
foreach ($plugins as $plugin) {
$pluginLoaded = new $plugin($event);
if (!$pluginLoaded instanceof Copy\Npm\PluginInterface) {
$this->io->write('Could not load ' . $plugin);
if ($pluginLoaded instanceof Copy\CopyFromNpmInterface) {
$pluginLoaded->processNpmInstall();
continue;
}
$pluginLoaded->processNpmInstall();
$this->io->write('Could not load ' . $plugin);
}
}

Expand Down

0 comments on commit 21cf8ee

Please sign in to comment.