Skip to content

Commit

Permalink
Merge main Composer config and Package file into one
Browse files Browse the repository at this point in the history
  • Loading branch information
coenjacobs committed Sep 8, 2024
1 parent e34a170 commit cb12e04
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 102 deletions.
62 changes: 45 additions & 17 deletions src/Composer/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@

use CoenJacobs\Mozart\Composer\Autoload\Autoloader;
use CoenJacobs\Mozart\Config\Autoload;
use CoenJacobs\Mozart\Config\Composer;
use CoenJacobs\Mozart\Config\ConfigAccessor;
use CoenJacobs\Mozart\Config\Extra;
use CoenJacobs\Mozart\Config\ReadsConfig;
use stdClass;

class Package
{
use ReadsConfig, ConfigAccessor;

/** @var string */
public $path = '';

/** @var Composer */
public $config;

/** @var Package[] */
public $requirePackages = [];

public string $name;

/** @var string[] */
public array $require;

public ?Autoload $autoload = null;
public ?Extra $extra = null;

/**
* Create a PHP object to represent a composer package.
*
Expand All @@ -26,42 +35,61 @@ class Package
* @param stdClass $overrideAutoload Optional configuration to replace the package's own autoload definition with
* another which Mozart can use.
*/
public function __construct($path, Composer $config = null, $overrideAutoload = null)
public function __construct($path, $overrideAutoload = null)
{
$this->path = $path;

if (empty($config)) {
$config = Composer::loadFromFile($this->path . '/composer.json');
}

$this->config = $config;

if (isset($overrideAutoload)) {
$autoload = new Autoload();
$autoload->setupAutoloaders($overrideAutoload);
$this->config->set('autoload', $autoload);
$this->set('autoload', $autoload);
}
}

public function setAutoload(stdClass $data): void
{
$autoload = new Autoload();
$autoload->setupAutoloaders($data);
$this->autoload = $autoload;
}

public function getExtra(): ?Extra
{
return $this->extra;
}

public function isValidMozartConfig(): bool
{
if (empty($this->getExtra())) {
return false;
}

if (empty($this->getExtra()->getMozart())) {
return false;
}

return $this->getExtra()->getMozart()->isValidMozartConfig();
}

public function getName(): string
{
return $this->config->name;
return $this->name;
}

/**
* @return Autoloader[]
*/
public function getAutoloaders(): array
{
if (empty($this->config->autoload)) {
if (empty($this->autoload)) {
return array();
}

return $this->config->autoload->getAutoloaders();
return $this->autoload->getAutoloaders();
}

/**
* @return array<Package>
* @return Package[]
*/
public function getDependencies(): array
{
Expand All @@ -74,7 +102,7 @@ public function registerRequirePackage(Package $package): void
}

/**
* @param array<Package> $packages
* @param Package[] $packages
*/
public function registerRequirePackages(array $packages): void
{
Expand Down
43 changes: 0 additions & 43 deletions src/Config/Composer.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/Config/PackageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CoenJacobs\Mozart\Config;

use CoenJacobs\Mozart\Composer\Package;
use stdClass;

class PackageFactory
{
public static function createPackage(string $path, stdClass $overrideAutoload = null): Package
{
$package = Package::loadFromFile($path);

if (! empty($overrideAutoload)) {
$package->setAutoload($overrideAutoload);
}

return $package;
}
}
32 changes: 13 additions & 19 deletions src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CoenJacobs\Mozart\Config\Mozart;
use CoenJacobs\Mozart\Config\Composer;
use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Config\PackageFactory;
use CoenJacobs\Mozart\Mover;
use CoenJacobs\Mozart\Replacer;
use Exception;
Expand Down Expand Up @@ -48,32 +49,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$composerFile = $workingDir . DIRECTORY_SEPARATOR. 'composer.json';
try {
$composerConfig = Composer::loadFromFile($composerFile);
$package = PackageFactory::createPackage($composerFile);
} catch (Exception $e) {
$output->write('Unable to read the composer.json file');
return 1;
}

if (! $composerConfig->isValidMozartConfig() || empty($composerConfig->getExtra())) {
if (! $package->isValidMozartConfig() || empty($package->getExtra())) {
$output->write('Mozart config not readable in composer.json at extra->mozart');
return 1;
}

$mozartConfig = $composerConfig->getExtra()->getMozart();
$config = $package->getExtra()->getMozart();

if (empty($mozartConfig)) {
if (empty($config)) {
$output->write('Mozart config not readable in composer.json at extra->mozart');
return 1;
}

$this->config = $mozartConfig;
$this->config = $config;
$this->config->set('dep_namespace', preg_replace("/\\\{2,}$/", "\\", $this->config->get('dep_namespace')."\\"));

$require = array();
if (is_array($this->config->get('packages'))) {
$require = $this->config->get('packages');
} else {
$require = $composerConfig->require;
$require = $package->require;
}

$packagesByName = $this->findPackages($require);
Expand All @@ -100,9 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

/**
* @param $workingDir
* @param $config
* @param array $packages
* @param Package[] $packages
*
* @return void
*/
Expand All @@ -116,9 +115,7 @@ protected function movePackages($packages): void
}

/**
* @param $workingDir
* @param $config
* @param array $packages
* @param Package[] $packages
*
* @return void
*/
Expand Down Expand Up @@ -187,7 +184,7 @@ private function findPackages(array $slugs): array
$autoloaders = $override_autoload->$package_slug;
}

$package = new Package($packageDir, null, $autoloaders);
$package = PackageFactory::createPackage($packageDir . 'composer.json', $autoloaders);

if ($this->config->isExcludedPackage($package)) {
continue;
Expand All @@ -204,8 +201,8 @@ private function findPackages(array $slugs): array

/**
* Get an array containing all the dependencies and dependencies
* @param Package $package
* @param array $dependencies
* @param Package $package
* @param Package[] $dependencies
* @return array
*/
private function getAllDependenciesOfPackage(Package $package, $dependencies = []): array
Expand All @@ -219,7 +216,6 @@ private function getAllDependenciesOfPackage(Package $package, $dependencies = [
return $dependencies;
}

/** @var Package $dependency */
foreach ($package->getDependencies() as $dependency) {
$dependencies[] = $dependency;
}
Expand All @@ -232,15 +228,13 @@ private function getAllDependenciesOfPackage(Package $package, $dependencies = [
}

/**
* @param array $packages
* @param Package[] $packages
*/
private function replaceParentInTree(array $packages): void
{
/** @var Package $package */
foreach ($packages as $package) {
$dependencies = $this->getAllDependenciesOfPackage($package);

/** @var Package $dependency */
foreach ($dependencies as $dependency) {
$this->replacer->replaceParentPackage($dependency, $package);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function deleteDepTargetDirs(Package $package): void
$this->filesystem->deleteDirectory($outputDir);
break;
case Classmap::class:
$outputDir = $this->config->getClassmapDirectory() . $package->config->get('name');
$outputDir = $this->config->getClassmapDirectory() . $package->get('name');
$outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir);
$this->filesystem->deleteDirectory($outputDir);
break;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function deleteEmptyDirs(): void
*/
public function movePackage(Package $package)
{
if (in_array($package->config->get('name'), $this->movedPackages)) {
if (in_array($package->get('name'), $this->movedPackages)) {
return;
}

Expand All @@ -118,7 +118,7 @@ public function movePackage(Package $package)

foreach ($autoloader->paths as $path) {
$source_path = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR
. $package->config->get('name') . DIRECTORY_SEPARATOR . $path;
. $package->get('name') . DIRECTORY_SEPARATOR . $path;

$source_path = str_replace('/', DIRECTORY_SEPARATOR, $source_path);

Expand All @@ -135,7 +135,7 @@ public function movePackage(Package $package)

foreach ($autoloader->files as $file) {
$source_path = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor'
. DIRECTORY_SEPARATOR . $package->config->get('name');
. DIRECTORY_SEPARATOR . $package->get('name');
$finder->files()->name($file)->in($source_path);

foreach ($finder as $foundFile) {
Expand All @@ -148,7 +148,7 @@ public function movePackage(Package $package)

foreach ($autoloader->paths as $path) {
$source_path = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor'
. DIRECTORY_SEPARATOR . $package->config->get('name') . DIRECTORY_SEPARATOR . $path;
. DIRECTORY_SEPARATOR . $package->get('name') . DIRECTORY_SEPARATOR . $path;

$finder->files()->in($source_path);

Expand All @@ -163,8 +163,8 @@ public function movePackage(Package $package)
}
}

if (!in_array($package->config->get('name'), $this->movedPackages)) {
$this->movedPackages[] = $package->config->get('name');
if (!in_array($package->get('name'), $this->movedPackages)) {
$this->movedPackages[] = $package->get('name');
}
}

Expand All @@ -187,16 +187,16 @@ public function moveFile(Package $package, $autoloader, $file, $path = '')
$replaceWith = $this->config->getDepDirectory() . $namespacePath;
$targetFile = str_replace($this->workingDir, $replaceWith, $file->getPathname());

$packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->config->get('name')
$packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->get('name')
. DIRECTORY_SEPARATOR . $path;
$packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath);
$targetFile = str_replace($packageVendorPath, '', $targetFile);
} else {
$namespacePath = $package->config->get('name');
$namespacePath = $package->get('name');
$replaceWith = $this->config->getClassmapDirectory() . $namespacePath;
$targetFile = str_replace($this->workingDir, $replaceWith, $file->getPathname());

$packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->config->get('name')
$packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->get('name')
. DIRECTORY_SEPARATOR;
$packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath);
$targetFile = str_replace($packageVendorPath, DIRECTORY_SEPARATOR, $targetFile);
Expand Down
Loading

0 comments on commit cb12e04

Please sign in to comment.