1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliQArts\Docweaver\Services\Product; |
6
|
|
|
|
7
|
|
|
use ReliQArts\Docweaver\Contracts\ConfigProvider; |
8
|
|
|
use ReliQArts\Docweaver\Contracts\Exception; |
9
|
|
|
use ReliQArts\Docweaver\Contracts\Filesystem; |
10
|
|
|
use ReliQArts\Docweaver\Contracts\Logger; |
11
|
|
|
use ReliQArts\Docweaver\Contracts\Product\Finder as FinderContract; |
12
|
|
|
use ReliQArts\Docweaver\Contracts\Product\Maker as ProductFactory; |
13
|
|
|
use ReliQArts\Docweaver\Models\Product; |
14
|
|
|
|
15
|
|
|
final class Finder implements FinderContract |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Filesystem |
19
|
|
|
*/ |
20
|
|
|
private $filesystem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Logger |
24
|
|
|
*/ |
25
|
|
|
private $logger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ConfigProvider |
29
|
|
|
*/ |
30
|
|
|
private $configProvider; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ProductFactory |
34
|
|
|
*/ |
35
|
|
|
private $productFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* ProductFinder constructor. |
39
|
|
|
* |
40
|
|
|
* @param Filesystem $filesystem |
41
|
|
|
* @param Logger $logger |
42
|
|
|
* @param ConfigProvider $configProvider |
43
|
|
|
* @param ProductFactory $productFactory |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
Filesystem $filesystem, |
47
|
|
|
Logger $logger, |
48
|
|
|
ConfigProvider $configProvider, |
49
|
|
|
ProductFactory $productFactory |
50
|
|
|
) { |
51
|
|
|
$this->filesystem = $filesystem; |
52
|
|
|
$this->logger = $logger; |
53
|
|
|
$this->configProvider = $configProvider; |
54
|
|
|
$this->productFactory = $productFactory; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param bool $includeUnknowns |
59
|
|
|
* |
60
|
|
|
* @return Product[] |
61
|
|
|
*/ |
62
|
|
|
public function listProducts(bool $includeUnknowns = false): array |
63
|
|
|
{ |
64
|
|
|
$products = []; |
65
|
|
|
$documentationDirectory = $this->configProvider->getDocumentationDirectory(); |
66
|
|
|
$productDirectories = $this->filesystem->directories(base_path($documentationDirectory)); |
67
|
|
|
|
68
|
|
|
foreach ($productDirectories as $productDirectory) { |
69
|
|
|
try { |
70
|
|
|
$product = $this->productFactory->create($productDirectory); |
71
|
|
|
if ($includeUnknowns || $product->getDefaultVersion() !== Product::VERSION_UNKNOWN) { |
72
|
|
|
$products[$product->getKey()] = $product; |
73
|
|
|
} |
74
|
|
|
} catch (Exception $e) { |
75
|
|
|
$this->logger->error($e->getMessage(), []); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $products; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $productName |
84
|
|
|
* |
85
|
|
|
* @return null|Product |
86
|
|
|
*/ |
87
|
|
|
public function findProduct(string $productName): ?Product |
88
|
|
|
{ |
89
|
|
|
$productKey = strtolower($productName); |
90
|
|
|
$products = $this->listProducts(); |
91
|
|
|
|
92
|
|
|
if (!array_key_exists($productKey, $products)) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $products[$productKey]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|