support json config
authorMichael Wallner <mike@php.net>
Fri, 11 Feb 2022 20:09:14 +0000 (21:09 +0100)
committerMichael Wallner <mike@php.net>
Fri, 11 Feb 2022 20:50:00 +0000 (21:50 +0100)
mdref/Repo.php

index f52a35e5ac06c7d939e4e3ad6d718ee61a2eefdf..80af9078dbf3b9a43945187e316b9495c1fdb376 100644 (file)
@@ -33,9 +33,27 @@ class Repo implements IteratorAggregate {
 
        /**
         * The edit url template
-        * @var string
+        * @var ?string
+        */
+       private $editUrl;
+
+       /**
+        * Origin url
+        * @var ?string
+        */
+       private $origin;
+
+       /**
+        * Git branch
+        * @var ?string
+        */
+       private $branch;
+
+       /**
+        * Comment provider configuration
+        * @var ?object
         */
-       private $edit;
+       private $commentProvider;
 
        /**
         * Path to the repository containing the name.mdref file
@@ -43,15 +61,33 @@ class Repo implements IteratorAggregate {
         * @throws \InvalidArgumentException
         */
        public function __construct(string $path) {
-               if (!($glob = glob("$path/*.mdref"))) {
-                       throw new InvalidArgumentException(
-                               sprintf("Not a reference, could not find '*.mdref': '%s'", $path));
+               if (!($glob = glob("$path/{mdref.*,*.mdref}", GLOB_BRACE))) {
+                       throw new InvalidArgumentException("Not a reference, could not find any '{mdref.*,*.mdref}': '$path'");
                }
 
-               $mdref = current($glob);
-               $this->path = realpath($path);
-               $this->name = basename($mdref, ".mdref");
-               $this->edit = trim(file_get_contents($mdref));
+               foreach ($glob as $mdref) {
+                       switch (pathinfo($mdref, PATHINFO_EXTENSION)) {
+                               case "mdref":
+                                       $this->name = basename($mdref, ".mdref");
+                                       $this->editUrl = trim(file_get_contents($mdref));
+                                       break;
+                               case "json":
+                                       $json = json_decode(file_get_contents($mdref));
+                                       if (!isset($json->name)) {
+                                               throw new InvalidArgumentException("Missing key 'path' in $mdref");
+                                       }
+                                       $this->name = $json->name;
+                                       foreach (["origin", "branch", "editUrl", "commentProvider"] as $key) {
+                                               if (isset($json->$key)) {
+                                                       $this->$key = $json->$key;
+                                               }
+                                       }
+                                       break;
+                               default:
+                                       throw InvalidArgumentException("Unknown $path");
+                       }
+                       $this->path = realpath($path);
+               }
        }
 
        /**
@@ -77,7 +113,28 @@ class Repo implements IteratorAggregate {
         * @return string
         */
        public function getEditUrl(string $entry) : string {
-               return sprintf($this->edit, $entry);
+               return sprintf($this->editUrl, $entry);
+       }
+
+       /**
+        * @return ?string
+        */
+       public function getBranch() {
+               return $this->branch;
+       }
+
+       /**
+        * @return ?string
+        */
+       public function getOrigin() {
+               return $this->origin;
+       }
+
+       /**
+        * @return ?object
+        */
+       public function getCommentProvider() {
+               return $this->commentProvider;
        }
 
        /**