Skip to content

Commit

Permalink
Update nette/di 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FVesely authored and Milan Felix Šulc committed May 17, 2019
1 parent 681b4f9 commit a45e1f6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": ">=7.1",
"dragonmantank/cron-expression": "^2.2.0",
"nette/di": "^2.4.12",
"nette/di": "~3.0.0",
"symfony/console": "^4.2.0"
},
"require-dev": {
Expand Down
16 changes: 10 additions & 6 deletions src/DI/SchedulerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@
use Nette\DI\CompilerExtension;
use Nette\DI\Helpers;
use Nette\DI\Statement;
use Nette\Schema\Expect;
use Nette\Schema\Schema;

class SchedulerExtension extends CompilerExtension
{

/** @var mixed[] */
private $defaults = [
'path' => '%tempDir%/scheduler',
'jobs' => [],
];
public function getConfigSchema(): Schema
{
return Expect::structure([
'path' => Expect::string('%tempDir%/scheduler'),
'jobs' => Expect::array(),
]);
}

/**
* Register services
*/
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);
$config = (array) $this->config;
$config = Helpers::expand($config, $builder->parameters);

// Scheduler
Expand Down
16 changes: 16 additions & 0 deletions tests/DI/CallbackJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Tests\Contributte\Scheduler\DI;

final class CallbackJob
{

public static function foo(): void
{
}

public function bar(): void
{
}

}
20 changes: 20 additions & 0 deletions tests/DI/CustomJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Tests\Contributte\Scheduler\DI;

use Contributte\Scheduler\IJob;
use DateTime;

final class CustomJob implements IJob
{

public function isDue(DateTime $dateTime): bool
{
return true;
}

public function run(): void
{
}

}
7 changes: 6 additions & 1 deletion tests/DI/SchedulerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public function testRegister(): void
],
]);
$compiler->addExtension('scheduler', new SchedulerExtension());
$compiler->loadConfig(__DIR__ . '/jobs.config.neon');
});
/** @var Container $container */
$container = new $class();
self::assertInstanceOf(IScheduler::class, $container->getByType(IScheduler::class));

$scheduler = $container->getByType(IScheduler::class);
self::assertInstanceOf(IScheduler::class, $scheduler);

self::assertCount(3, $scheduler->getAll());
}

}
8 changes: 8 additions & 0 deletions tests/DI/jobs.config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
callbackJob: Tests\Contributte\Scheduler\DI\CallbackJob

scheduler:
jobs:
- {cron: '* * * * *', callback: Tests\Contributte\Scheduler\DI\CallbackJob::foo}
- {cron: '* * * * *', callback: [@callbackJob, bar]}
- Tests\Contributte\Scheduler\DI\CustomJob

0 comments on commit a45e1f6

Please sign in to comment.