forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core/Dependencies: render resolved tree in php
- Loading branch information
Showing
6 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ILIAS\Core\Dependencies; | ||
|
||
/** | ||
* An object that looks like a Dependency Injection Container but actually | ||
* does nothing. | ||
*/ | ||
class NullDIC implements \ArrayAccess | ||
{ | ||
public function offsetSet($id, $value): void | ||
{ | ||
} | ||
|
||
public function offsetGet($id): null | ||
{ | ||
return null; | ||
} | ||
|
||
public function offsetExists($id): false | ||
{ | ||
return false; | ||
} | ||
|
||
public function offsetUnset($id): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ILIAS\Core\Dependencies; | ||
|
||
/** | ||
* This takes a (hopefully resolved...) dependency tree and renders it in PHP to | ||
* be used for initialisation. | ||
*/ | ||
class Renderer | ||
{ | ||
public function render(OfComponent ...$components): string | ||
{ | ||
return | ||
$this->renderHeader() . | ||
join("\n", array_map($this->renderComponent(...), $components)); | ||
} | ||
|
||
protected function renderHeader(): string | ||
{ | ||
return <<<PHP | ||
<?php | ||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
\$null_dic = new ILIAS\Core\Dependencies\NullDIC; | ||
PHP; | ||
} | ||
|
||
protected function renderComponent(OfComponent $component): string | ||
{ | ||
return <<<PHP | ||
\$component = new {$component->getComponentName()}(); | ||
\$implement = []; | ||
\$use = []; | ||
\$contribute = []; | ||
\$seek = []; | ||
\$provide = []; | ||
\$pull = []; | ||
\$internal = []; | ||
\$component->init(\$null_dic, \$implement, \$use, \$contribute, \$seek, \$provide, \$pull); | ||
PHP; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ILIAS\Core\Tests\Dependencies; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ILIAS\Core\Dependencies\Name; | ||
|
||
class NullDICTest extends TestCase | ||
{ | ||
public function testExistence() | ||
{ | ||
$dic = new \ILIAS\Core\Dependencies\NullDIC(); | ||
$this->assertInstanceOf(\ArrayAccess::class, $dic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ILIAS\Core\Tests\Dependencies; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ILIAS\Core\Dependencies\Reader; | ||
use ILIAS\Core\Dependencies\Resolver; | ||
use ILIAS\Core\Dependencies\Renderer; | ||
use ILIAS\Core\Component; | ||
|
||
class RendererTest extends TestCase | ||
{ | ||
protected Reader $reader; | ||
protected Resolver $resolver; | ||
protected Renderer $renderer; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->reader = new Reader(); | ||
$this->resolver = new Resolver(); | ||
$this->renderer = new Renderer(); | ||
} | ||
|
||
/** | ||
* @dataProvider scenarios | ||
*/ | ||
public function testScenario($scenario_file, $result_file, $components, $disambiguation) | ||
{ | ||
require_once(__DIR__ . "/scenarios/$scenario_file"); | ||
|
||
$components = array_map(fn($c) => $this->reader->read(new $c()), $components); | ||
$resolved = $this->resolver->resolveDependencies($disambiguation, ...$components); | ||
|
||
$result = $this->renderer->render(...$resolved); | ||
|
||
$expected = file_get_contents(__DIR__ . "/scenarios/$result_file"); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function scenarios() | ||
{ | ||
return [ | ||
"no dependencies" => ["scenario1.php", "result1.php", | ||
[ | ||
\ILIAS\Core\Tests\Dependencies\Scenario1\ComponentA::class | ||
], | ||
[] | ||
] | ||
]; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
components/ILIAS/Core/tests/Dependencies/scenarios/result1.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
$null_dic = new ILIAS\Core\Dependencies\NullDIC(); | ||
|
||
|
||
$component = new ILIAS\Core\Tests\Dependencies\Scenario1\ComponentA(); | ||
|
||
$implement = []; | ||
$use = []; | ||
$contribute = []; | ||
$seek = []; | ||
$provide = []; | ||
$pull = []; | ||
$internal = []; | ||
|
||
$component->init($null_dic, $implement, $use, $contribute, $seek, $provide, $pull); |
36 changes: 36 additions & 0 deletions
36
components/ILIAS/Core/tests/Dependencies/scenarios/scenario1.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
namespace ILIAS\Core\Tests\Dependencies\Scenario1; | ||
|
||
use ILIAS\Core\Component; | ||
|
||
class ComponentA implements Component | ||
{ | ||
public function init( | ||
array | \ArrayAccess &$define, | ||
array | \ArrayAccess &$implement, | ||
array | \ArrayAccess &$use, | ||
array | \ArrayAccess &$contribute, | ||
array | \ArrayAccess &$seek, | ||
array | \ArrayAccess &$provide, | ||
array | \ArrayAccess &$pull, | ||
array | \ArrayAccess &$internal, | ||
): void { | ||
} | ||
} |