From 91ff6d7683a1833a7b0758f6de6c26325b4ee708 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Fri, 27 Sep 2024 08:52:00 +0200 Subject: [PATCH] Component: add Activities --- cli/entry_point.php | 39 ++++ components/ILIAS/Component/Component.php | 11 + .../Component/src/Activities/Activity.php | 74 +++++++ .../Component/src/Activities/ActivityImpl.php | 33 +++ .../Component/src/Activities/ActivityType.php | 27 +++ .../Activities/ListActivitiesEntryPoint.php | 44 ++++ .../ILIAS/Component/src/Activities/Query.php | 32 +++ .../ILIAS/Component/src/Activities/README.md | 190 ++++++++++++++++++ .../Component/src/Activities/Repository.php | 34 ++++ .../src/Activities/StaticRepository.php | 71 +++++++ .../tests/Activities/StaticRepositoryTest.php | 167 +++++++++++++++ components/ILIAS/Setup/Setup.php | 3 + .../ILIAS/Setup/src/Activities/GetStatus.php | 48 +++++ 13 files changed, 773 insertions(+) create mode 100644 cli/entry_point.php create mode 100644 components/ILIAS/Component/src/Activities/Activity.php create mode 100644 components/ILIAS/Component/src/Activities/ActivityImpl.php create mode 100644 components/ILIAS/Component/src/Activities/ActivityType.php create mode 100644 components/ILIAS/Component/src/Activities/ListActivitiesEntryPoint.php create mode 100644 components/ILIAS/Component/src/Activities/Query.php create mode 100644 components/ILIAS/Component/src/Activities/README.md create mode 100644 components/ILIAS/Component/src/Activities/Repository.php create mode 100644 components/ILIAS/Component/src/Activities/StaticRepository.php create mode 100644 components/ILIAS/Component/tests/Activities/StaticRepositoryTest.php create mode 100644 components/ILIAS/Setup/src/Activities/GetStatus.php diff --git a/cli/entry_point.php b/cli/entry_point.php new file mode 100644 index 000000000000..b38d55b4e5c2 --- /dev/null +++ b/cli/entry_point.php @@ -0,0 +1,39 @@ + + new Component\Activities\StaticRepository( + $seek[Component\Activities\Activity::class] + ); + $contribute[Component\EntryPoint::class] = static fn() => new Component\EntryPoint\HelloWorld("Component/HelloWorld"); + $contribute[Component\EntryPoint::class] = static fn() => + new Component\Activities\ListActivitiesEntryPoint( + $use[Component\Activities\Repository::class] + ); $contribute[\ILIAS\Setup\Agent::class] = static fn() => new \ilComponentsSetupAgent( diff --git a/components/ILIAS/Component/src/Activities/Activity.php b/components/ILIAS/Component/src/Activities/Activity.php new file mode 100644 index 000000000000..4c054ee0b2e3 --- /dev/null +++ b/components/ILIAS/Component/src/Activities/Activity.php @@ -0,0 +1,74 @@ +repository->getActivitiesByName("/.*/")))); + echo "\n"; + return 0; + } +} diff --git a/components/ILIAS/Component/src/Activities/Query.php b/components/ILIAS/Component/src/Activities/Query.php new file mode 100644 index 000000000000..e4636a126566 --- /dev/null +++ b/components/ILIAS/Component/src/Activities/Query.php @@ -0,0 +1,32 @@ + where keys are the name + */ + public function getActivitiesByName(string $name_matcher, ?ActivityType $type = null, ?Range $range = null): \Iterator; +} diff --git a/components/ILIAS/Component/src/Activities/StaticRepository.php b/components/ILIAS/Component/src/Activities/StaticRepository.php new file mode 100644 index 000000000000..f02846cc175f --- /dev/null +++ b/components/ILIAS/Component/src/Activities/StaticRepository.php @@ -0,0 +1,71 @@ + $ativities + */ + public function __construct( + array $activities + ) { + foreach ($activities as $activity) { + if (!($activity instanceof Activity)) { + throw new \InvalidArgumentException( + "Expected `Activity`, got: " . get_class($activity) + ); + } + $this->activities[(string) $activity->getName()] = $activity; + } + } + + public function getActivitiesByName(string $name_matcher, ?ActivityType $type = null, ?Range $range = null): \Iterator + { + if ($range !== null) { + [$start, $length] = $range->unpack(); + } else { + [$start, $length] = [0, PHP_INT_MAX]; + } + + foreach ($this->activities as $name => $activity) { + if ($type !== null && $activity->getType() !== $type) { + continue; + } + if (preg_match($name_matcher, $name)) { + if ($start <= 0) { + $length--; + yield $name => $activity; + } + } + + $start--; + + if ($length === 0) { + break; + } + } + } +} diff --git a/components/ILIAS/Component/tests/Activities/StaticRepositoryTest.php b/components/ILIAS/Component/tests/Activities/StaticRepositoryTest.php new file mode 100644 index 000000000000..79b72534f994 --- /dev/null +++ b/components/ILIAS/Component/tests/Activities/StaticRepositoryTest.php @@ -0,0 +1,167 @@ +assertEquals([], iterator_to_array($repository->getActivitiesByName("%.*%"))); + } + + public function testKeyIsName(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturn(new Name($name)); + + $repository = new StaticRepository([$activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%.*%")); + + $this->assertEquals($activity, $activities[$name]); + } + + public function testGetActivitiesByNameNoMatch(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturn(new Name($name)); + + $repository = new StaticRepository([$activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%Foo%")); + + $this->assertEquals([], $activities); + } + + public function testGetActivitiesByNameMatch(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturn(new Name($name)); + + $repository = new StaticRepository([$activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%.*Some.*%")); + + $this->assertEquals($activity, $activities[$name]); + } + + public function testGetActivitiesByTypeNoMatch(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturn(new Name($name)); + $activity + ->method("getType") + ->willReturn(ActivityType::Command); + + $repository = new StaticRepository([$activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%.*%", ActivityType::Query)); + + $this->assertEquals([], $activities); + } + + public function testGetActivitiesByTypeMatch(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturn(new Name($name)); + $activity + ->method("getType") + ->willReturn(ActivityType::Command); + + $repository = new StaticRepository([$activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%.*%", ActivityType::Command)); + + $this->assertEquals([$activity], array_values($activities)); + } + + public function testGetActivitiesInRange(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturnOnConsecutiveCalls( + new Name($name . "1"), + new Name($name . "2"), + new Name($name . "3"), + new Name($name . "4"), + new Name($name . "5") + ); + + $repository = new StaticRepository([$activity, $activity, $activity, $activity, $activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%.*%", null, new \ILIAS\Data\Range(0, 3))); + + $this->assertEquals([$activity, $activity, $activity], array_values($activities)); + } + + public function testGetActivitiesInRange2(): void + { + $name = "\\ILIAS\\Component\\Tests\\SomeActivity"; + + $activity = $this->createMock(Activity::class); + $activity + ->method("getName") + ->willReturnOnConsecutiveCalls( + new Name($name . "1"), + new Name($name . "2"), + new Name($name . "3"), + new Name($name . "4"), + new Name($name . "5") + ); + + $repository = new StaticRepository([$activity, $activity, $activity, $activity, $activity]); + + $activities = iterator_to_array($repository->getActivitiesByName("%.*%", null, new \ILIAS\Data\Range(3, 3))); + + $this->assertEquals([$activity, $activity], array_values($activities)); + } +} diff --git a/components/ILIAS/Setup/Setup.php b/components/ILIAS/Setup/Setup.php index a9f43efbbddf..7588849e00b3 100644 --- a/components/ILIAS/Setup/Setup.php +++ b/components/ILIAS/Setup/Setup.php @@ -50,6 +50,9 @@ public function init( $pull[\ILIAS\Data\Factory::class] ); + $contribute[\ILIAS\Component\Activities\Activity::class] = static fn() => + new \ILIAS\Setup\Activities\GetStatus(); + $internal["command.install"] = static fn() => new \ILIAS\Setup\CLI\InstallCommand( $internal["agent_finder"], diff --git a/components/ILIAS/Setup/src/Activities/GetStatus.php b/components/ILIAS/Setup/src/Activities/GetStatus.php new file mode 100644 index 000000000000..1257af6e2fe9 --- /dev/null +++ b/components/ILIAS/Setup/src/Activities/GetStatus.php @@ -0,0 +1,48 @@ +