Skip to content

Commit

Permalink
Refactor file structure; make Arch, OS and Stability configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 18, 2024
1 parent f360c65 commit ea19878
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Internal\DLoad;

use Internal\DLoad\Module\Common\Architecture;
use Internal\DLoad\Module\Common\Internal\Injection\ConfigLoader;
use Internal\DLoad\Module\Common\OperatingSystem;
use Internal\DLoad\Module\Common\Stability;
use Internal\DLoad\Module\Container\Internal\ConfigLoader;
use Internal\DLoad\Service\Container;

/**
Expand All @@ -21,7 +21,7 @@ private function __construct(
private Container $container,
) {}

public static function init(Container $container = new Module\Container\Internal\Container()): self
public static function init(Container $container = new Module\Common\Internal\Container()): self
{
return new self($container);
}
Expand Down
30 changes: 13 additions & 17 deletions src/Command/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -29,17 +30,11 @@ final class Get extends Command implements SignalableCommandInterface

public function configure(): void
{
$this->addArgument(
'binary',
InputArgument::REQUIRED,
'Binary name, e.g. "rr", "dolt", "temporal" etc.',
);
$this->addArgument(
'path',
InputArgument::OPTIONAL,
'Path to store the binary, e.g. "./bin"',
".",
);
$this->addArgument('binary', InputArgument::REQUIRED, 'Binary name, e.g. "rr", "dolt", "temporal" etc.');
$this->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Path to store the binary, e.g. "./bin"', ".");
$this->addOption('arch', null, InputOption::VALUE_OPTIONAL, 'Architecture, e.g. "amd64", "arm64" etc.');
$this->addOption('os', null, InputOption::VALUE_OPTIONAL, 'Operating system, e.g. "linux", "darwin" etc.');
$this->addOption('stability', null, InputOption::VALUE_OPTIONAL, 'Stability, e.g. "stable", "beta" etc.');
}

public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
Expand Down Expand Up @@ -71,7 +66,7 @@ protected function execute(
OutputInterface $output,
): int {
$output->writeln('Binary to load: ' . $input->getArgument('binary'));
$output->writeln('Path to store the binary: ' . $input->getArgument('path'));
$output->writeln('Path to store the binary: ' . $input->getOption('path'));

$container = Bootstrap::init()->withConfig(
xml: \dirname(__DIR__, 2) . '/dload.xml',
Expand All @@ -85,11 +80,12 @@ protected function execute(
$output->writeln('Stability: ' . $container->get(Stability::class)->name);


$repo = 'roadrunner-server/roadrunner';
trap(GitHubRepository::fromDsn($repo)->getReleases()->first()->getAssets()
->whereArchitecture($container->get(Architecture::class))
->whereOperatingSystem($container->get(OperatingSystem::class))
);
// $repo = 'roadrunner-server/roadrunner';
// trap(
// GitHubRepository::fromDsn($repo)->getReleases()->first()->getAssets()
// ->whereArchitecture($container->get(Architecture::class))
// ->whereOperatingSystem($container->get(OperatingSystem::class)),
// );


return Command::SUCCESS;
Expand Down
5 changes: 3 additions & 2 deletions src/Module/Common/Architecture.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Internal\DLoad\Module\Common;

use Internal\DLoad\Module\Common\Config\BuildInput;
use Internal\DLoad\Service\Factoriable;

/**
Expand All @@ -18,9 +19,9 @@ enum Architecture: string implements Factoriable

private const ERROR_UNKNOWN_ARCH = 'Current architecture `%s` may not be supported.';

public static function create(): static
public static function create(BuildInput $config): self
{
return self::fromGlobals();
return self::tryFrom((string) $config->arch) ?? self::fromGlobals();
}

public static function fromGlobals(): self
Expand Down
23 changes: 23 additions & 0 deletions src/Module/Common/Config/BuildInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Internal\DLoad\Module\Common\Config;

use Internal\DLoad\Module\Common\Internal\Attribute\InputOption;

/**
* @internal
* @psalm-internal Internal\DLoad\Module\Common
*/
final class BuildInput
{
#[InputOption('arch')]
public ?string $arch = null;

#[InputOption('stability')]
public ?string $stability = null;

#[InputOption('os')]
public ?string $os = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Internal;
namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Attribute;

use Internal\DLoad\Module\Container\Internal\ConfigAttribute;
namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Attribute;

use Internal\DLoad\Module\Container\Internal\ConfigAttribute;
namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Attribute;

use Internal\DLoad\Module\Container\Internal\ConfigAttribute;
namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Attribute;

use Internal\DLoad\Module\Container\Internal\ConfigAttribute;
namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Attribute;

use Internal\DLoad\Module\Container\Internal\ConfigAttribute;
namespace Internal\DLoad\Module\Common\Internal\Attribute;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Internal;
namespace Internal\DLoad\Module\Common\Internal;

use Internal\DLoad\Module\Common\Internal\Injection\ConfigLoader;
use Internal\DLoad\Service\Container as AppContainerInterface;
use Internal\DLoad\Service\Destroyable;
use Internal\DLoad\Service\Factoriable;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function make(string $class, array $arguments = []): object
$binding = $this->factory[$class] ?? null;

if ($binding instanceof \Closure) {
$result = $binding($this);
$result = $this->injector->invoke($binding);
} else {
try {
$result = $this->injector->make($class, \array_merge((array) $binding, $arguments));
Expand All @@ -97,7 +98,7 @@ public function make(string $class, array $arguments = []): object

// Detect related types
// Configs
if (\str_starts_with($class, 'Internal\\DLoad\\Config\\')) {
if (\str_starts_with($class, 'Internal\\DLoad\\Module\\Common\\Config\\')) {
// Hydrate config
/** @var ConfigLoader $configLoader */
$configLoader = $this->get(ConfigLoader::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

declare(strict_types=1);

namespace Internal\DLoad\Module\Container\Internal;

use Internal\DLoad\Module\Container\Attribute\Env;
use Internal\DLoad\Module\Container\Attribute\InputArgument;
use Internal\DLoad\Module\Container\Attribute\InputOption;
use Internal\DLoad\Module\Container\Attribute\PhpIni;
use Internal\DLoad\Module\Container\Attribute\XPath;
namespace Internal\DLoad\Module\Common\Internal\Injection;

use Internal\DLoad\Module\Common\Internal\Attribute\ConfigAttribute;
use Internal\DLoad\Module\Common\Internal\Attribute\Env;
use Internal\DLoad\Module\Common\Internal\Attribute\InputArgument;
use Internal\DLoad\Module\Common\Internal\Attribute\InputOption;
use Internal\DLoad\Module\Common\Internal\Attribute\PhpIni;
use Internal\DLoad\Module\Common\Internal\Attribute\XPath;
use Internal\DLoad\Service\Logger;

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Module/Common/OperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Internal\DLoad\Module\Common;

use Internal\DLoad\Module\Common\Config\BuildInput;
use Internal\DLoad\Service\Factoriable;

/**
Expand All @@ -21,9 +22,9 @@ enum OperatingSystem: string implements Factoriable

private const ERROR_UNKNOWN_OS = 'Current OS `%s` may not be supported';

public static function create(): static
public static function create(BuildInput $config): static
{
return self::fromGlobals();
return self::tryFrom((string) $config->os) ?? self::fromGlobals();
}

public static function fromGlobals(): self
Expand Down
5 changes: 3 additions & 2 deletions src/Module/Common/Stability.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Internal\DLoad\Module\Common;

use Internal\DLoad\Module\Common\Config\BuildInput;
use Internal\DLoad\Service\Factoriable;

/**
Expand All @@ -19,9 +20,9 @@ enum Stability: string implements Factoriable
case Alpha = 'alpha';
case Dev = 'dev';

public static function create(): static
public static function create(BuildInput $config): static
{
return self::fromGlobals();
return self::tryFrom((string) $config->stability) ?? self::fromGlobals();
}

public static function fromGlobals(): self
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Repository/Internal/GitHub/GitHubRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(string $owner, string $repository, HttpClientInterfa
*/
public static function fromDsn(string $package, HttpClientInterface $client = null): GitHubRepository
{
[$owner, $name] = explode('/', $package);
[$owner, $name] = \explode('/', $package);
return new GitHubRepository($owner, $name, $client);
}

Expand Down
7 changes: 3 additions & 4 deletions src/Service/Factoriable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
/**
* Class creates new instances of itself.
*
* @method static create
*
* @internal
*/
interface Factoriable
{
public static function create(): static;
}
interface Factoriable {}

0 comments on commit ea19878

Please sign in to comment.