Debian Codegraph @

vendor/jfcherng/php-diff/src/Factory/LineRendererFactory.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jfcherng\Diff\Factory;
6
7 use Jfcherng\Diff\Renderer\Html\LineRenderer\AbstractLineRenderer;
8 use Jfcherng\Diff\Renderer\RendererConstant;
9
10 final class LineRendererFactory
11 {
12 /**
13 * Instances of line renderers.
14 *
15 * @var AbstractLineRenderer[]
16 */
17 private static array $singletons = [];
18
19 /**
20 * The constructor.
21 */
22 private function __construct()
23 {
24 }
25
26 /**
27 * Get the singleton of a line renderer.
28 *
29 * @param string $type the type
30 * @param mixed ...$ctorArgs the constructor arguments
31 */
32 public static function getInstance(string $type, ...$ctorArgs): AbstractLineRenderer
33 {
34 return self::$singletons[$type] ??= self::make($type, ...$ctorArgs);
35 }
36
37 /**
38 * Make a new instance of a line renderer.
39 *
40 * @param string $type the type
41 * @param mixed ...$ctorArgs the constructor arguments
42 *
43 * @throws \InvalidArgumentException
44 */
45 public static function make(string $type, ...$ctorArgs): AbstractLineRenderer
46 {
47 $className = RendererConstant::RENDERER_NAMESPACE . '\\Html\\LineRenderer\\' . ucfirst($type);
48
49 if (!class_exists($className)) {
50 throw new \InvalidArgumentException("LineRenderer not found: {$type}");
51 }
52
53 return new $className(...$ctorArgs);
54 }
55 }