|
| 1 | +<?php |
| 2 | + |
| 3 | +use Twig\Extension\AbstractExtension; |
| 4 | +use Twig\TwigFilter; |
| 5 | +use Twig\TwigFunction; |
| 6 | + |
| 7 | +class MarkdownTwigExtension extends AbstractExtension |
| 8 | +{ |
| 9 | + const NAMESPACE_LINKS = [ |
| 10 | + 'ORM\\' => '/orm/reference', |
| 11 | + 'Illuminate\\Support\\' => '/orm/reference', |
| 12 | + ]; |
| 13 | + public function getFilters() |
| 14 | + { |
| 15 | + return [ |
| 16 | + new TwigFilter('linkClasses', [$this, 'linkClasses'], ['is_safe' => ['all']]), |
| 17 | + new TwigFilter('lcFirst', 'lcfirst'), |
| 18 | + new TwigFilter('join', function ($value, $glue = ', ') { |
| 19 | + if (is_array($value)) { |
| 20 | + return implode($glue, $value); |
| 21 | + } |
| 22 | + |
| 23 | + if ($value instanceof \phpDocumentor\Descriptor\Collection) { |
| 24 | + return implode($glue, $value->getAll()); |
| 25 | + } |
| 26 | + |
| 27 | + return $value; |
| 28 | + }), |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + public function getFunctions() |
| 33 | + { |
| 34 | + return [ |
| 35 | + new TwigFunction('generateSignature', [$this, 'generateSignature'], ['is_safe' => ['html']]), |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + public function linkClasses($value) |
| 40 | + { |
| 41 | + return preg_replace_callback( |
| 42 | + '/\\\\([A-Za-z0-9_\\\\]+)/', |
| 43 | + function ($match) { |
| 44 | + $type = $match[1]; |
| 45 | + foreach (self::NAMESPACE_LINKS as $namespace => $link) { |
| 46 | + if (strncmp($type, $namespace, strlen($namespace)) === 0) { |
| 47 | + return '[' . $type . '](' . $link . '/' . str_replace('\\', '/', $type) . ')'; |
| 48 | + } |
| 49 | + } |
| 50 | + return $type; |
| 51 | + }, |
| 52 | + $value |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param \phpDocumentor\Descriptor\MethodDescriptor $method |
| 58 | + */ |
| 59 | + public function generateSignature($method, $maxLength = null) |
| 60 | + { |
| 61 | + $signature = 'function ' . $method->getName() . '('; |
| 62 | + $args = []; |
| 63 | + |
| 64 | + foreach ($method->getArguments() as $argument) { |
| 65 | + $arg = '$' . $argument->getName(); |
| 66 | + if ($argument->isByReference()) { |
| 67 | + $arg = '&' . $arg; |
| 68 | + } |
| 69 | + if ($argument->getType() !== 'mixed') { |
| 70 | + $arg = $argument->getType() . ' ' . $arg; |
| 71 | + } |
| 72 | + if ($argument->getDefault()) { |
| 73 | + $arg = $arg . ' = ' . $argument->getDefault(); |
| 74 | + } |
| 75 | + $args[] = $arg; |
| 76 | + } |
| 77 | + |
| 78 | + $signature .= implode(', ', $args) . ')'; |
| 79 | + |
| 80 | + if ($method->isStatic()) { |
| 81 | + $signature = 'static ' . $signature; |
| 82 | + } |
| 83 | + |
| 84 | + $signature = $method->getVisibility() . ' ' . $signature; |
| 85 | + |
| 86 | + if ($method->isAbstract()) { |
| 87 | + $signature = 'abstract ' . $signature; |
| 88 | + } |
| 89 | + |
| 90 | + if ($method->isFinal()) { |
| 91 | + $signature = 'final ' . $signature; |
| 92 | + } |
| 93 | + |
| 94 | + if ($method->getResponse() && $method->getName() !== '__construct') { |
| 95 | + $type = $method->getResponse()->getType(); |
| 96 | + if ($type === 'self') { |
| 97 | + $signature .= ': ' . $method->getParent()->getName(); |
| 98 | + } else { |
| 99 | + $signature .= ': ' . $type; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if ($maxLength && strlen($signature) > $maxLength) { |
| 104 | + if (preg_match('/^(.*)\((.*)\)($|:.*$)/', $signature, $matches)) { |
| 105 | + $lines = [$matches[1] . '(']; |
| 106 | + $args = explode(', ', $matches[2]); |
| 107 | + foreach ($args as $arg) { |
| 108 | + $lines[] = ' ' . $arg . ', '; |
| 109 | + } |
| 110 | + $lines[] = ')' . $matches[3]; |
| 111 | + $signature = implode("\n", $lines); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return $signature; |
| 116 | + } |
| 117 | +} |
0 commit comments