1<?php
2
3namespace dokuwiki\plugin\issuelinks\classes;
4
5use dokuwiki\plugin\issuelinks\services\ServiceInterface;
6
7class ServiceProvider
8{
9
10    protected static $instance;
11    protected $serviceClasses = [];
12
13    protected function __construct()
14    {
15        $serviceDir = __DIR__ . '/../services';
16        $filenames = scandir($serviceDir, SCANDIR_SORT_ASCENDING);
17        foreach ($filenames as $filename) {
18            if ($filename === '.' || $filename === '..') {
19                continue;
20            }
21            list($service, $servicePostfix) = explode('.', $filename, 2);
22            if ($servicePostfix !== 'service.php') {
23                continue;
24            }
25            require_once $serviceDir . '/' . $filename;
26
27            $serviceClass = '\\dokuwiki\\plugin\\issuelinks\\services\\' . $service;
28            $this->serviceClasses[$serviceClass::ID] = $serviceClass;
29        }
30    }
31
32    public static function getInstance($forcereload = false)
33    {
34        if (null === self::$instance || $forcereload) {
35            self::$instance = new self();
36        }
37        return self::$instance;
38    }
39
40    public function getSyntaxKeys()
41    {
42        $keys = [];
43        foreach ($this->serviceClasses as $className) {
44            $syntax = $className::SYNTAX;
45
46            $keys[$syntax] = $className;
47        }
48
49        return $keys;
50    }
51
52    /**
53     * @return ServiceInterface[]
54     */
55    public function getWebHookUserAgents()
56    {
57        $userAgents = [];
58        foreach ($this->serviceClasses as $className) {
59            $ua = $className::WEBHOOK_UA;
60
61            $userAgents[$ua] = $className;
62        }
63
64        return $userAgents;
65    }
66
67    /**
68     * @return ServiceInterface[]]
69     */
70    public function getServices()
71    {
72        return $this->serviceClasses;
73    }
74}
75