这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/devkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Zalas\Toolbox\Cli\Runner;
use Zalas\Toolbox\Json\JsonTools;
use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\ShCommand;
use Zalas\Toolbox\Tool\Tool;
Expand All @@ -30,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$jsonPath = $input->getOption('tools');
$readmePath = $input->getOption('readme');
$tools = (new Tools($jsonPath))->all();
$tools = (new JsonTools($jsonPath))->all();
$toolsList = $tools->reduce('', function ($acc, Tool $tool) {
return $acc . sprintf('* %s - [%s](%s)', $tool->name(), $tool->summary(), $tool->website()) . PHP_EOL;
});
Expand Down
13 changes: 8 additions & 5 deletions depfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ layers:
collectors:
- type: className
regex: ^Zalas\\Toolbox\\Cli\\.*
- name: Json
collectors:
- type: className
regex: ^Zalas\\Toolbox\\Json\\.*
- name: Tool
collectors:
- type: className
Expand All @@ -32,21 +36,20 @@ layers:
must_not:
# must not be one of the known vendors
- type: className
regex: ^Zalas\\Toolbox\\Cli\\.*
- type: className
regex: ^Zalas\\Toolbox\\Tool\\.*
- type: className
regex: ^Zalas\\Toolbox\\UseCase\\.*
regex: ^Zalas\\Toolbox\\(Cli|Json|Tool|UseCase)\\.*
- type: className
regex: ^Psr\\Container\\.*
- type: className
regex: ^Symfony\\Component\\Console\\.*
ruleset:
Cli:
- Tool
- Json
- UseCase
- Symfony Console
- Psr Container
Json:
- Tool
Tool:
UseCase:
- Tool
3 changes: 2 additions & 1 deletion src/Cli/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Zalas\Toolbox\Cli\Command\InstallCommand;
use Zalas\Toolbox\Cli\Command\ListCommand;
use Zalas\Toolbox\Cli\Command\TestCommand;
use Zalas\Toolbox\Json\JsonTools;
use Zalas\Toolbox\Tool\Tools;
use Zalas\Toolbox\UseCase\InstallTools;
use Zalas\Toolbox\UseCase\ListTools;
Expand Down Expand Up @@ -102,6 +103,6 @@ private function createTestToolsUseCase(): TestTools

private function createTools(): Tools
{
return new Tools($this->getParameter('toolbox_json'));
return new JsonTools($this->getParameter('toolbox_json'));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Tool\Command;
namespace Zalas\Toolbox\Json\Factory;

final class Assert
{
Expand Down
16 changes: 16 additions & 0 deletions src/Json/Factory/BoxBuildCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\BoxBuildCommand;

final class BoxBuildCommandFactory
{
public static function import(array $definition): Command
{
Assert::requireFields(['repository', 'phar', 'bin'], $definition, 'BoxBuildCommand');

return new BoxBuildCommand($definition['repository'], $definition['phar'], $definition['bin'], $definition['version'] ?? null);
}
}
16 changes: 16 additions & 0 deletions src/Json/Factory/ComposerBinPluginCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\ComposerBinPluginCommand;

final class ComposerBinPluginCommandFactory
{
public static function import(array $command): Command
{
Assert::requireFields(['package', 'namespace'], $command, 'ComposerBinPluginCommand');

return new ComposerBinPluginCommand($command['package'], $command['namespace']);
}
}
16 changes: 16 additions & 0 deletions src/Json/Factory/ComposerGlobalInstallCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\ComposerGlobalInstallCommand;

final class ComposerGlobalInstallCommandFactory
{
public static function import(array $command): Command
{
Assert::requireFields(['package'], $command, 'ComposerGlobalInstallCommand');

return new ComposerGlobalInstallCommand($command['package']);
}
}
16 changes: 16 additions & 0 deletions src/Json/Factory/ComposerInstallCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\ComposerInstallCommand;

final class ComposerInstallCommandFactory
{
public static function import(array $command): Command
{
Assert::requireFields(['repository'], $command, 'ComposerInstallCommand');

return new ComposerInstallCommand($command['repository'], $command['version'] ?? null);
}
}
16 changes: 16 additions & 0 deletions src/Json/Factory/FileDownloadCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\FileDownloadCommand;

final class FileDownloadCommandFactory
{
public static function import(array $command): Command
{
Assert::requireFields(['url', 'file'], $command, 'FileDownloadCommand');

return new FileDownloadCommand($command['url'], $command['file']);
}
}
16 changes: 16 additions & 0 deletions src/Json/Factory/PharDownloadCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\PharDownloadCommand;

final class PharDownloadCommandFactory
{
public static function import(array $command): Command
{
Assert::requireFields(['phar', 'bin'], $command, 'PharDownloadCommand');

return new PharDownloadCommand($command['phar'], $command['bin']);
}
}
56 changes: 56 additions & 0 deletions src/Json/Factory/ToolFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json\Factory;

use Zalas\Toolbox\Tool\Collection;
use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\MultiStepCommand;
use Zalas\Toolbox\Tool\Command\TestCommand;
use Zalas\Toolbox\Tool\Tool;

final class ToolFactory
{
public static function import(array $tool): Tool
{
Assert::requireFields(['name', 'summary', 'website', 'command', 'test'], $tool, 'tool');

return new Tool($tool['name'], $tool['summary'], $tool['website'], self::importCommand($tool), new TestCommand($tool['test'], $tool['name']));
}

private static function importCommand(array $tool): Command
{
$commands = Collection::create([]);

foreach ($tool['command'] as $type => $command) {
$commands = $commands->merge(self::createCommands($type, $command));
}

if (0 === $commands->count()) {
throw new \RuntimeException(\sprintf('No valid command defined for the tool: %s', \json_encode($tool)));
}

return 1 === $commands->count() ? $commands->toArray()[0] : new MultiStepCommand($commands);
}

private static function createCommands($type, $command): Collection
{
$factories = [
'phar-download' => \sprintf('%s::import', PharDownloadCommandFactory::class),
'file-download' => \sprintf('%s::import', FileDownloadCommandFactory::class),
'box-build' => \sprintf('%s::import', BoxBuildCommandFactory::class),
'composer-install' => \sprintf('%s::import', ComposerInstallCommandFactory::class),
'composer-global-install' => \sprintf('%s::import', ComposerGlobalInstallCommandFactory::class),
'composer-bin-plugin' => \sprintf('%s::import', ComposerBinPluginCommandFactory::class),
];

if (!isset($factories[$type])) {
throw new \RuntimeException(\sprintf('Unrecognised command: "%s". Supported commands are: "%s".', $type, \implode(', ', \array_keys($factories))));
}

$command = !\is_numeric(\key($command)) ? [$command] : $command;

return Collection::create(\array_map(function ($c) use ($type, $factories) {
return $factories[$type]($c);
}, $command));
}
}
82 changes: 82 additions & 0 deletions src/Json/JsonTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php declare(strict_types=1);

namespace Zalas\Toolbox\Json;

use InvalidArgumentException;
use RuntimeException;
use Zalas\Toolbox\Json\Factory\ToolFactory;
use Zalas\Toolbox\Tool\Collection;
use Zalas\Toolbox\Tool\Command\ShCommand;
use Zalas\Toolbox\Tool\Command\TestCommand;
use Zalas\Toolbox\Tool\Tool;
use Zalas\Toolbox\Tool\Tools;

final class JsonTools implements Tools
{
/**
* @var string
*/
private $resource;

public function __construct(string $resource)
{
if (!\is_readable($resource)) {
throw new InvalidArgumentException(\sprintf('Could not read the file: "%s".', $resource));
}

$this->resource = $resource;
}

/**
* @return Collection|Tool[]
*/
public function all(): Collection
{
return $this->defaultTools()
->merge(Collection::create(
\array_map(\sprintf('%s::import', ToolFactory::class), $this->loadJson())
));
}

private function loadJson(): array
{
$json = \json_decode(\file_get_contents($this->resource), true);

if (!$json) {
throw new RuntimeException(\sprintf('Failed to parse json: "%s"', $this->resource));
}

if (!isset($json['tools']) || !\is_array($json['tools'])) {
throw new RuntimeException(\sprintf('Failed to find any tools in: "%s".', $this->resource));
}

return $json['tools'];
}

private function defaultTools(): Collection
{
return Collection::create([
new Tool(
'composer',
'Dependency Manager for PHP',
'https://getcomposer.org/',
new ShCommand('composer self-update'),
new TestCommand('composer list', 'composer')
),
new Tool(
'composer-bin-plugin',
'Composer plugin to install bin vendors in isolated locations',
'https://github.com/bamarni/composer-bin-plugin',
new ShCommand('composer global require bamarni/composer-bin-plugin'),
new TestCommand('composer global show bamarni/composer-bin-plugin', 'composer-bin-plugin')
),
new Tool(
'box',
'An application for building and managing Phars',
'https://box-project.github.io/box2/',
new ShCommand('curl -Ls https://box-project.github.io/box2/installer.php | php && mv box.phar /usr/local/bin/box && chmod +x /usr/local/bin/box'),
new TestCommand('box list', 'box')
),
]);
}
}
9 changes: 1 addition & 8 deletions src/Tool/Command/BoxBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class BoxBuildCommand implements Command
private $bin;
private $version;

private function __construct(string $repository, string $phar, string $bin, ?string $version = null)
public function __construct(string $repository, string $phar, string $bin, ?string $version = null)
{
$this->repository = $repository;
$this->phar = $phar;
Expand All @@ -33,13 +33,6 @@ public function __toString(): string
);
}

public static function import(array $definition): Command
{
Assert::requireFields(['repository', 'phar', 'bin'], $definition, 'BoxBuildCommand');

return new self($definition['repository'], $definition['phar'], $definition['bin'], $definition['version'] ?? null);
}

private function targetDir(): string
{
$targetDir = \preg_replace('#^.*/(.*?)(.git)?$#', '$1', $this->repository);
Expand Down
7 changes: 0 additions & 7 deletions src/Tool/Command/ComposerBinPluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ public function __toString(): string
return \sprintf('composer global bin %s require --no-suggest --prefer-dist --update-no-dev -n %s', $this->namespace, $this->package);
}

public static function import(array $command): Command
{
Assert::requireFields(['package', 'namespace'], $command, 'ComposerBinPluginCommand');

return new self($command['package'], $command['namespace']);
}

public function package(): string
{
return $this->package;
Expand Down
7 changes: 0 additions & 7 deletions src/Tool/Command/ComposerGlobalInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ public function __toString(): string
return \sprintf('composer global require --no-suggest --prefer-dist --update-no-dev -n %s', $this->package);
}

public static function import(array $command): Command
{
Assert::requireFields(['package'], $command, 'ComposerGlobalInstallCommand');

return new self($command['package']);
}

public function package(): string
{
return $this->package;
Expand Down
7 changes: 0 additions & 7 deletions src/Tool/Command/ComposerInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ public function __toString(): string
);
}

public static function import(array $command): Command
{
Assert::requireFields(['repository'], $command, 'ComposerInstallCommand');

return new self($command['repository'], $command['version'] ?? null);
}

private function targetDir(): string
{
$targetDir = \preg_replace('#^.*/(.*?)(.git)?$#', '$1', $this->repository);
Expand Down
Loading