Page MenuHomeDevCentral

D2547.id6429.diff
No OneTemporary

D2547.id6429.diff

diff --git a/Makefile b/Makefile
new file mode 100644
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,49 @@
+# -------------------------------------------------------------
+# Makefile for Keruald monorepo
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Keruald
+# Description: Generate repository needed files to start dev
+# License: Trivial work, not eligible to copyright
+# -------------------------------------------------------------
+
+RM=rm -f
+RMDIR=rm -rf
+
+GENERATED_FROM_TEMPLATES=phpcs.xml phpunit.xml
+
+# -------------------------------------------------------------
+# Main targets
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+all: build-dev build-repo
+
+clean: clean-dev clean-repo
+
+regenerate: clean-repo build-repo clean-dev build-dev
+
+# -------------------------------------------------------------
+# Build targets for libraries development
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+build-dev: vendor
+
+clean-dev:
+ ${RMDIR} vendor
+
+vendor:
+ composer update
+
+# -------------------------------------------------------------
+# Build targets for monorepo maintenance
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+build-repo: $(GENERATED_FROM_TEMPLATES) composer.json
+
+clean-repo:
+ ${RM} $(GENERATED_FROM_TEMPLATES) composer.json
+
+$(GENERATED_FROM_TEMPLATES):
+ _utils/templates/resolve.py _templates/$@.in > $@
+
+composer.json:
+ _utils/templates/generate-compose-json.php > composer.json
diff --git a/_utils/templates/generate-compose-json.php b/_utils/templates/generate-compose-json.php
new file mode 100755
--- /dev/null
+++ b/_utils/templates/generate-compose-json.php
@@ -0,0 +1,95 @@
+#!/usr/bin/env php
+<?php
+
+# -------------------------------------------------------------
+# Generate composer.json from packages metadata
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Keruald
+# License: BSD-2-Clause
+# Dependencies: symfony/yaml, keruald/omnitools
+# -------------------------------------------------------------
+
+use Symfony\Component\Yaml\Parser as YamlParser;
+use Keruald\OmniTools\Collections\HashMap;
+
+require_once __DIR__ . "/../../vendor/autoload.php";
+
+# -------------------------------------------------------------
+# Helper methods to build elements from metadata
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+function getAutoload(array $packages_namespaces) : array {
+ return [
+ "psr-4" => HashMap::from($packages_namespaces)
+ ->flatMap(fn($package, $ns) => [
+ $ns . '\\' => $package . "/src/",
+ $ns . '\\Tests\\' => $package . "/tests/",
+ ])
+ ->toArray()
+ ];
+}
+
+function getReplace(array $packages) : array {
+ return HashMap::from($packages)
+ ->mapValuesAndKeys(fn($v) => ["keruald/$v", "self.version"])
+ ->toArray();
+}
+
+# -------------------------------------------------------------
+# Template
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+function run() : never {
+ $metadata = (new YamlParser())->parseFile("metadata.yml");
+ $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
+
+ echo json_encode(getTemplate($metadata), $flags), "\n";
+
+ exit(0);
+}
+
+function getTemplate(array $metadata) : array {
+ return [
+ "name" => "keruald/keruald",
+ "type" => "library",
+ "description" => "Modular libraries to build frameworks and applications",
+ "keywords" => [
+ "framework",
+ "keruald",
+ ],
+ "license" => "BSD-2-Clause",
+ "homepage" => "https://keruald.nasqueron.org",
+ "authors" => [
+ [
+ "name" => "Sébastien Santoro",
+ "email" => "dereckson@espace-win.org",
+ ],
+ [
+ "name" => "Keruald contributors",
+ ],
+ ],
+ "require" => [
+ "ext-intl" => "*",
+ ],
+ "require-dev" => [
+ "ext-mbstring" => "*",
+ "ext-mysqli" => "*",
+ "ext-xmlwriter" => "*",
+ "nasqueron/codestyle" => "^0.0.1",
+ "phan/phan" => "^5.3.1",
+ "phpunit/phpunit" => "^9.5",
+ "symfony/yaml" => "^6.0.3",
+ "squizlabs/php_codesniffer" => "^3.6",
+ ],
+ "replace" => getReplace($metadata["packages"]),
+ "autoload" => getAutoload($metadata["packages_namespaces"]),
+ "scripts" => [
+ "lint-src" => "find */src -type f -name '*.php' | xargs -n1 php -l",
+ "lint-tests" => "find */tests -type f -name '*.php' | xargs -n1 php -l",
+ "test" => "vendor/bin/phpunit",
+ ],
+ "minimum-stability" => "dev",
+ ];
+}
+
+run();
diff --git a/composer.json b/composer.json
new file mode 100644
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,57 @@
+{
+ "name": "keruald/keruald",
+ "type": "library",
+ "description": "Modular libraries to build frameworks and applications",
+ "keywords": [
+ "framework",
+ "keruald"
+ ],
+ "license": "BSD-2-Clause",
+ "homepage": "https://keruald.nasqueron.org",
+ "authors": [
+ {
+ "name": "Sébastien Santoro",
+ "email": "dereckson@espace-win.org"
+ },
+ {
+ "name": "Keruald contributors"
+ }
+ ],
+ "require": {
+ "ext-intl": "*"
+ },
+ "require-dev": {
+ "ext-mbstring": "*",
+ "ext-mysqli": "*",
+ "ext-xmlwriter": "*",
+ "nasqueron/codestyle": "^0.0.1",
+ "phan/phan": "^5.3.1",
+ "phpunit/phpunit": "^9.5",
+ "symfony/yaml": "^6.0.3",
+ "squizlabs/php_codesniffer": "^3.6"
+ },
+ "replace": {
+ "keruald/commands": "self.version",
+ "keruald/database": "self.version",
+ "keruald/omnitools": "self.version",
+ "keruald/report": "self.version"
+ },
+ "autoload": {
+ "psr-4": {
+ "Keruald\\Commands\\": "commands/src/",
+ "Keruald\\Commands\\Tests\\": "commands/tests/",
+ "Keruald\\Database\\": "database/src/",
+ "Keruald\\Database\\Tests\\": "database/tests/",
+ "Keruald\\OmniTools\\": "omnitools/src/",
+ "Keruald\\OmniTools\\Tests\\": "omnitools/tests/",
+ "Keruald\\Reporting\\": "report/src/",
+ "Keruald\\Reporting\\Tests\\": "report/tests/"
+ }
+ },
+ "scripts": {
+ "lint-src": "find */src -type f -name '*.php' | xargs -n1 php -l",
+ "lint-tests": "find */tests -type f -name '*.php' | xargs -n1 php -l",
+ "test": "vendor/bin/phpunit"
+ },
+ "minimum-stability": "dev"
+}
diff --git a/omnitools/src/Collections/BaseCollection.php b/omnitools/src/Collections/BaseCollection.php
--- a/omnitools/src/Collections/BaseCollection.php
+++ b/omnitools/src/Collections/BaseCollection.php
@@ -11,6 +11,12 @@
public static abstract function from (iterable $items) : static;
+ ///
+ /// Constants
+ ///
+
+ const CB_ZERO_ARG = "Callback function should have at least one argument";
+
///
/// Getters
///
diff --git a/omnitools/src/Collections/HashMap.php b/omnitools/src/Collections/HashMap.php
--- a/omnitools/src/Collections/HashMap.php
+++ b/omnitools/src/Collections/HashMap.php
@@ -154,12 +154,10 @@
public function filter (callable $callable) : self {
$argc = (new CallableElement($callable))->countArguments();
if ($argc === 0) {
- throw new InvalidArgumentException(
- "Callback should have at least one argument"
- );
+ throw new InvalidArgumentException(self::CB_ZERO_ARG);
}
- $mode = (int)($argc > 1);
+ $mode = (int)($argc > 1);
return new self(
array_filter($this->map, $callable, $mode)
);
@@ -174,6 +172,38 @@
return new self($mappedMap);
}
+ public function mapValuesAndKeys (callable $callable) : self {
+ $argc = (new CallableElement($callable))->countArguments();
+
+ $newMap = [];
+ foreach ($this->map as $key => $value) {
+ [$newKey, $newValue] = match($argc) {
+ 0 => throw new InvalidArgumentException(self::CB_ZERO_ARG),
+ 1 => $callable($value),
+ default => $callable($key, $value),
+ };
+ $newMap[$newKey] = $newValue;
+ }
+
+ return new self($newMap);
+ }
+
+ public function flatMap (callable $callable) : self {
+ $argc = (new CallableElement($callable))->countArguments();
+
+ $newMap = new self;
+ foreach ($this->map as $key => $value) {
+ $toAdd = match($argc) {
+ 0 => throw new InvalidArgumentException(self::CB_ZERO_ARG),
+ 1 => $callable($value),
+ default => $callable($key, $value),
+ };
+ $newMap->update($toAdd);
+ }
+
+ return $newMap;
+ }
+
public function filterKeys (callable $callable) : self {
return new self(
array_filter($this->map, $callable, ARRAY_FILTER_USE_KEY)
diff --git a/omnitools/src/Collections/Vector.php b/omnitools/src/Collections/Vector.php
--- a/omnitools/src/Collections/Vector.php
+++ b/omnitools/src/Collections/Vector.php
@@ -202,6 +202,22 @@
return new self($mappedVector);
}
+ public function flatMap (callable $callable) : self {
+ $argc = (new CallableElement($callable))->countArguments();
+
+ $newMap = new self;
+ foreach ($this->items as $key => $value) {
+ $toAdd = match($argc) {
+ 0 => throw new InvalidArgumentException(self::CB_ZERO_ARG),
+ 1 => $callable($value),
+ default => $callable($key, $value),
+ };
+ $newMap->append($toAdd);
+ }
+
+ return $newMap;
+ }
+
public function filterKeys (callable $callable) : self {
return new self(
array_filter($this->items, $callable, ARRAY_FILTER_USE_KEY)

File Metadata

Mime Type
text/plain
Expires
Mon, Aug 4, 11:24 (18 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2867491
Default Alt Text
D2547.id6429.diff (10 KB)

Event Timeline