Skip to content

Commit

Permalink
Fix config object implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
coenjacobs committed Jul 23, 2024
1 parent 6bfeea0 commit 5c08c14
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
15 changes: 8 additions & 7 deletions src/Composer/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

class Config
{
/** @var array */
/** @var array<mixed> */
private $data;

/**
* @param array $data
* @param array<mixed> $data
*/
public function __construct(array $data)
{
$this->data = $data;
}

public static function loadFromFile($filePath)
public static function loadFromFile(string $filePath): Config
{
$config = json_decode(file_get_contents($filePath));
return new self($config);
$fileContents = file_get_contents($filePath);
$config = ( ! $fileContents ) ? $config = array() : json_decode( $fileContents );
return new self((array)$config);
}

/**
* @param string $key
* @return bool|mixed
*/
public function get($key)
public function get(string $key)
{
if (isset($this->data[ $key ])) {
return $this->data[ $key ];
Expand All @@ -38,7 +39,7 @@ public function get($key)
* @param string $key
* @param mixed $data
*/
public function set($key, $data)
public function set($key, $data): void
{
$this->data[$key] = $data;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Composer/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Package
*/
public function __construct($path, Config $config = null, $overrideAutoload = null)
{
$this->path = $path;
$this->path = $path;

if (isset($config)) {
if (empty($config)) {
$config = Config::loadFromFile($this->path . '/composer.json');
}
$this->config = $config;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function findAutoloaders()
}
}

public function setConfig(Config $config)
public function setConfig(Config $config): void
{
$this->config = $config;
}
Expand Down
20 changes: 9 additions & 11 deletions src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
$config = $composer->extra->mozart;

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

$require = array();
if (isset($config->packages) && is_array($config->packages)) {
$require = $config->packages;
if (is_array($this->config->get('packages'))) {
$require = $this->config->get('packages');
} elseif (isset($composer->require) && is_object($composer->require)) {
$require = array_keys(get_object_vars($composer->require));
}

$packagesByName = $this->findPackages($require);
$excludedPackagesNames = isset($config->excluded_packages) ? $config->excluded_packages : [];
$excludedPackagesNames = is_array($this->config->get('excluded_packages')) ? $this->config->get('excluded_packages') : [];
$packagesToMoveByName = array_diff_key($packagesByName, array_flip($excludedPackagesNames));
$packages = array_values($packagesToMoveByName);

foreach ($packages as $package) {
$package->dependencies = array_diff_key($package->dependencies, array_flip($excludedPackagesNames));
}

$this->mover = new Mover($workingDir, $config);
$this->replacer = new Replacer($workingDir, $config);
$this->mover = new Mover($workingDir, $this->config);
$this->replacer = new Replacer($workingDir, $this->config);

$require = $config->get('packages');
if ($require !== false) {
$require = array_keys(get_object_vars($this->config->get('require')));
}
$require = $this->config->get('packages');
$require = (is_array($require)) ? array_keys($require) : array();

$packages = $this->findPackages($require);

Expand Down
61 changes: 31 additions & 30 deletions tests/MoverTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
declare(strict_types=1);

use CoenJacobs\Mozart\Composer\Config;
use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Console\Commands\Compose;
use CoenJacobs\Mozart\Mover;
Expand All @@ -22,7 +23,7 @@ class MoverTest extends TestCase
/**
* composer->extra->mozart settings
*
* @var stdClass
* @var Config
*/
protected $config;

Expand All @@ -38,23 +39,23 @@ public function setUp(): void
mkdir($this->testsWorkingDir);
}

$config = new class() {
};
$config->dep_directory = "/dep_directory/";
$config->classmap_directory = "/classmap_directory/";
$config->packages = array(
"pimple/pimple",
"ezyang/htmlpurifier"
);

$pimpleAutoload = json_decode("{ \"psr-0\" : { \"Pimple\" : [ \"src/\" ] } }");
$htmlpurifierAutoload = json_decode("{ \"classmap\" : { \"Pimple\" => [ \"library/\" ] } }");

$config->override_autoload = array();
$config->override_autoload["pimple/pimple"] = $pimpleAutoload;
$config->override_autoload["ezyang/htmlpurifier"] = $htmlpurifierAutoload;
$configArgs = array(
'dep_directory' => "/dep_directory/",
'classmap_directory' => "/classmap_directory/",
'packages' => array(
"pimple/pimple",
"ezyang/htmlpurifier",
),
'override_autoload' => array(
'pimple/pimple' => $pimpleAutoload,
'ezyang/htmlpurifier' => $htmlpurifierAutoload,
),
);

$this->config = $config;
$this->config = new Config( $configArgs );
}

/**
Expand All @@ -72,9 +73,9 @@ public function it_creates_absent_dirs(): void
$mover->deleteTargetDirs($packages);

$this->assertTrue(file_exists($this->testsWorkingDir . DIRECTORY_SEPARATOR
. $this->config->dep_directory));
. $this->config->get('dep_directory')));
$this->assertTrue(file_exists($this->testsWorkingDir . DIRECTORY_SEPARATOR
. $this->config->classmap_directory));
. $this->config->get('classmap_directory')));
}

/**
Expand All @@ -87,15 +88,15 @@ public function it_is_unpertrubed_by_existing_dirs(): void
{
$mover = new Mover($this->testsWorkingDir, $this->config);

if (!file_exists($this->testsWorkingDir . $this->config->dep_directory)) {
mkdir($this->testsWorkingDir . $this->config->dep_directory);
if (!file_exists($this->testsWorkingDir . $this->config->get('dep_directory'))) {
mkdir($this->testsWorkingDir . $this->config->get('dep_directory'));
}
if (!file_exists($this->testsWorkingDir . $this->config->classmap_directory)) {
mkdir($this->testsWorkingDir . $this->config->classmap_directory);
if (!file_exists($this->testsWorkingDir . $this->config->get('classmap_directory'))) {
mkdir($this->testsWorkingDir . $this->config->get('classmap_directory'));
}

$this->assertDirectoryExists($this->testsWorkingDir . $this->config->dep_directory);
$this->assertDirectoryExists($this->testsWorkingDir . $this->config->classmap_directory);
$this->assertDirectoryExists($this->testsWorkingDir . $this->config->get('dep_directory'));
$this->assertDirectoryExists($this->testsWorkingDir . $this->config->get('classmap_directory'));

$packages = array();

Expand All @@ -119,30 +120,30 @@ public function it_deletes_subdirs_for_packages_about_to_be_moved(): void
{
$mover = new Mover($this->testsWorkingDir, $this->config);

mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->dep_directory);
mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->classmap_directory);
mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->get('dep_directory'));
mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->get('classmap_directory'));

mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->dep_directory . 'Pimple');
mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->classmap_directory . 'ezyang');
mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->get('dep_directory') . 'Pimple');
mkdir($this->testsWorkingDir . DIRECTORY_SEPARATOR . $this->config->get('classmap_directory') . 'ezyang');

$packages = array();
foreach ($this->config->packages as $packageString) {
foreach ($this->config->get('packages') as $packageString) {
$testDummyComposerDir = $this->testsWorkingDir . DIRECTORY_SEPARATOR . 'vendor'
. DIRECTORY_SEPARATOR . $packageString;
@mkdir($testDummyComposerDir, 0777, true);
$testDummyComposerPath = $testDummyComposerDir . DIRECTORY_SEPARATOR . 'composer.json';
$testDummyComposerContents = json_encode(new stdClass());

file_put_contents($testDummyComposerPath, $testDummyComposerContents);
$parsedPackage = new Package($testDummyComposerDir, $this->config->override_autoload[$packageString]);
$parsedPackage = new Package($testDummyComposerDir, $this->config, $this->config->get('override_autoload')[$packageString]);
$parsedPackage->findAutoloaders();
$packages[] = $parsedPackage;
}

$mover->deleteTargetDirs($packages);

$this->assertDirectoryDoesNotExist($this->testsWorkingDir . $this->config->dep_directory . 'Pimple');
$this->assertDirectoryDoesNotExist($this->testsWorkingDir . $this->config->dep_directory . 'ezyang');
$this->assertDirectoryDoesNotExist($this->testsWorkingDir . $this->config->get('dep_directory') . 'Pimple');
$this->assertDirectoryDoesNotExist($this->testsWorkingDir . $this->config->get('dep_directory') . 'ezyang');
}

/**
Expand Down

0 comments on commit 5c08c14

Please sign in to comment.