-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGetDocumentationPageUrl.php
63 lines (53 loc) · 1.75 KB
/
GetDocumentationPageUrl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace BumbleDocGen\Core\Renderer\Twig\Function;
use BumbleDocGen\Core\Configuration\Exception\InvalidConfigurationParameterException;
use BumbleDocGen\Core\Renderer\Breadcrumbs\BreadcrumbsHelper;
use DI\DependencyException;
use DI\NotFoundException;
use Psr\Log\LoggerInterface;
/**
* Creates an entity link by object
*
* @example {{ getDocumentationPageUrl('Page name') }}
* @example {{ getDocumentationPageUrl('/someDir/someTemplate.md.twig') }}
* @example {{ getDocumentationPageUrl('/docs/someDir/someDocFile.md') }}
* @example {{ getDocumentationPageUrl('readme.md') }}
*/
final class GetDocumentationPageUrl implements CustomFunctionInterface
{
public const DEFAULT_URL = '#';
public function __construct(
private BreadcrumbsHelper $breadcrumbsHelper,
private LoggerInterface $logger,
) {
}
public static function getName(): string
{
return 'getDocumentationPageUrl';
}
public static function getOptions(): array
{
return [
'is_safe' => ['html'],
];
}
/**
* @param string $key The key by which to look up the URL of the page.
* Can be the title of a page, a path to a template, or a generated document
*
* @return string URL of the document, if found, otherwise DEFAULT_URL
*
* @throws DependencyException
* @throws InvalidConfigurationParameterException
* @throws NotFoundException
*/
public function __invoke(string $key): string
{
$pageLink = $this->breadcrumbsHelper->getPageLinkByKey($key);
if (!$pageLink) {
$this->logger->warning("Key `{$key}` not found to get document link.");
}
return $pageLink ?: self::DEFAULT_URL;
}
}