Skip to content

Commit

Permalink
enabled lazy instances, fixed opt/arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalkaoz committed Jan 24, 2017
1 parent efc6622 commit fa2f498
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/Console/ApiBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@

namespace IPFS\Console;

use IPFS\Api\ApiBuilder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ApiBuildCommand extends Command
{
/**
* @var ApiBuilder
* @var callable
*/
private $builder;

public function __construct(ApiBuilder $builder)
public function __construct(callable $builder)
{
parent::__construct(null);
$this->builder = $builder;
Expand All @@ -41,7 +40,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->builder->build();
$this->builder->__invoke()->build();

$output->writeln('updated Api Classes in <info>src/Api</info>');
}
Expand Down
8 changes: 5 additions & 3 deletions src/Console/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ private function createCode(Api\Api $api, $name, \ReflectionMethod $method): \Cl
};
}

public function addDriver(Driver $driver): CommandBuilder
public function addDriver(string $name, callable $driver): CommandBuilder
{
$this->drivers[get_class($driver)] = $driver;
$this->drivers[$name] = $driver;

return $this;
}
Expand All @@ -142,7 +142,9 @@ private function chooseClient(string $class): Driver
throw new \InvalidArgumentException(sprintf('"%s" is an unknown Driver, please add it with "addDriver"', $class));
}

return $this->drivers[$class];
//the driver is injected as Closure to allow lazy instantiation

return $this->drivers[$class]->__invoke();
}

private function sanitizeArguments(array $args): array
Expand Down
17 changes: 10 additions & 7 deletions src/Container/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $pimple)
{
$this->registerDriver($pimple);
$this->registerHttp($pimple);
$this->registerConsole($pimple);

$pimple[AnnotationReader::class] = function () {
Expand All @@ -52,7 +51,9 @@ public function register(Container $pimple)
);
};

$pimple[Api\ApiBuilder::class] = function (Container $pimple) {
$pimple[Api\ApiBuilder::class] = function () use ($pimple) {
$this->registerHttp($pimple);

$parser = new Api\ApiParser($pimple[HttpAsyncClient::class], $pimple[MessageFactory::class], new Crawler());
$generator = new Api\ApiGenerator(new BuilderFactory());

Expand Down Expand Up @@ -80,7 +81,9 @@ public function registerHttp(Container $pimple)

private function registerDriver(Container $pimple)
{
$pimple[Http::class] = function (Container $pimple) {
$pimple[Http::class] = function () use ($pimple) {
$this->registerHttp($pimple);

return new Http(
$pimple[HttpAsyncClient::class],
$pimple[MessageFactory::class],
Expand All @@ -90,7 +93,7 @@ private function registerDriver(Container $pimple)
);
};

$pimple[Cli::class] = function (Container $pimple) {
$pimple[Cli::class] = function () use ($pimple) {
return new Cli(new ProcessBuilder(), $pimple[AnnotationReader::class], getenv('IPFS_BINARY') ?: 'ipfs');
};
}
Expand Down Expand Up @@ -120,16 +123,16 @@ private function registerConsole(Container $pimple)
new Api\Tour(),
], $pimple[AnnotationReader::class]);

$builder->addDriver($pimple[Cli::class]);
$builder->addDriver($pimple[Http::class]);
$builder->addDriver(Cli::class, $pimple->raw(Cli::class));
$builder->addDriver(Http::class, $pimple->raw(Http::class));

return $builder;
};

$pimple[Application::class] = function (Container $pimple) {
$app = new Application('ipfs', '@git-version@');
$app->addCommands($pimple[CommandBuilder::class]->generateCommands());
$app->add(new ApiBuildCommand($pimple[Api\ApiBuilder::class]));
$app->add(new ApiBuildCommand($pimple->raw(Api\ApiBuilder::class)));

return $app;
};
Expand Down
2 changes: 0 additions & 2 deletions src/Driver/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ private function parseParameters(Command $command): array
$parsedParameters[] = $value;
continue;
}

throw new \LogicException(sprintf('"%s" is neither an option nor an argument', $name));
}

return $parsedParameters;
Expand Down
8 changes: 5 additions & 3 deletions tests/spec/Console/ApiBuildCommandSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ApiBuildCommandSpec extends ObjectBehavior
{
public function let(ApiBuilder $builder)
{
$this->beConstructedWith($builder);
$this->beConstructedWith(function () use ($builder) {
return $builder->getWrappedObject();
});
}

public function it_is_initializable()
Expand All @@ -38,8 +40,8 @@ public function it_has_a_descriptive_name()

public function it_calls_the_builder(ApiBuilder $builder, InputInterface $input, OutputInterface $output)
{
$builder->build()->shouldBeCalled();

$this->run($input, $output);

$builder->build()->shouldHaveBeenCalled();
}
}
4 changes: 3 additions & 1 deletion tests/spec/Console/CommandBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public function it_builds_symfony_commands_from_the_apis()

public function it_invokes_the_driver_on_run(Driver $driver, OutputInterface $output)
{
$this->getWrappedObject()->addDriver($driver->getWrappedObject());
$this->getWrappedObject()->addDriver(get_class($driver->getWrappedObject()), function () use ($driver) {
return $driver->getWrappedObject();
});

$command = $this->generateCommands()['test:foo'];
$command->setApplication(new Application());
Expand Down

0 comments on commit fa2f498

Please sign in to comment.