1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI\Definitions\Traits; |
19
|
|
|
|
20
|
|
|
use PhpParser\BuilderFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This trait adds deprecation functionality to the service definition. |
24
|
|
|
* |
25
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
trait DeprecationTrait |
28
|
|
|
{ |
29
|
|
|
/** @var array<string,string> */ |
30
|
|
|
private array $deprecation = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function deprecate(string $package = '', float $version = null, string $message = null) |
36
|
|
|
{ |
37
|
|
|
$this->deprecation['package'] = $package; |
38
|
|
|
$this->deprecation['version'] = $version ?? ''; |
39
|
|
|
|
40
|
|
|
if (!empty($message) && !\str_contains($message, '%service_id%')) { |
41
|
|
|
throw new \InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->deprecation['message'] = $message ?? 'The "%service_id%" service is deprecated. avoid using it, as it will be removed in the future.'; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function isDeprecated(): bool |
53
|
|
|
{ |
54
|
|
|
return !empty($this->deprecation); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getDeprecation(string $id = null): array |
61
|
|
|
{ |
62
|
|
|
if (isset($this->deprecation['message'])) { |
63
|
|
|
$this->deprecation['message'] = \str_replace('%service_id%', $id ?? $this->innerId ?? 'definition', $this->deprecation['message']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->deprecation; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Triggers a silenced deprecation notice. |
71
|
|
|
* |
72
|
|
|
* @param string $id |
73
|
|
|
* @param BuilderFactory|null $builder |
74
|
|
|
* |
75
|
|
|
* @return \PhpParser\Node\Expr\FuncCall|null |
76
|
|
|
*/ |
77
|
|
|
public function triggerDeprecation(string $id, ?BuilderFactory $builder = null) |
78
|
|
|
{ |
79
|
|
|
if ([] !== $deprecation = $this->getDeprecation($id)) { |
80
|
|
|
\trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']); |
81
|
|
|
|
82
|
|
|
if (null !== $builder) { |
83
|
|
|
return $builder->funcCall('\trigger_deprecation', \array_values($deprecation)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|