PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / vendor / zbateson / mail-mime-parser / src / Container.php

Container.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress trunk, at vendor/zbateson/mail-mime-parser/src/Container.php

127 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the ZBateson\MailMimeParser project.
4 *
5 * @license http://opensource.org/licenses/bsd-license.php BSD
6 */
7
8 namespace ZBateson\MailMimeParser;
9
10 use Pimple\Container as PimpleContainer;
11 use Pimple\Exception\UnknownIdentifierException;
12 use ReflectionClass;
13 use ReflectionParameter;
14
15 /**
16 * Automatically configures classes and dependencies.
17 *
18 * Sets up an automatic registration for classes when requested through
19 * Pimple by looking up the class's constructor and arguments.
20 *
21 * @author Zaahid Bateson
22 */
23 class Container extends PimpleContainer
24 {
25 /**
26 * Looks up the type of the passed ReflectionParameter and returns it as a
27 * fully qualified class name as expected by the class's auto registration.
28 *
29 * Null is returned for built-in types.
30 *
31 */
32 private function getParameterClass(ReflectionParameter $param) : ?string
33 {
34 if (\method_exists($param, 'getType')) {
35 $type = $param->getType();
36 if ($type && !$type->isBuiltin()) {
37 return \method_exists($type, 'getName') ? $type->getName() : (string) $type;
38 }
39 } elseif ($param->getClass() !== null) {
40 return $param->getClass()->getName();
41 }
42 return null;
43 }
44
45 /**
46 * Returns a factory function for the passed class.
47 *
48 * The returned factory method looks up arguments and uses pimple to get an
49 * instance of those types to pass them during construction.
50 */
51 public function autoRegister($class) : ?string
52 {
53 $fn = function($c) use ($class) {
54 $ref = new ReflectionClass($class);
55 $cargs = ($ref->getConstructor() !== null) ? $ref->getConstructor()->getParameters() : [];
56 $ap = [];
57 foreach ($cargs as $arg) {
58 $name = $arg->getName();
59 $argClass = $this->getParameterClass($arg);
60 if (!empty($c[$name])) {
61 $ap[] = $c[$name];
62 } elseif ($argClass !== null && !empty($c[$argClass])) {
63 $ap[] = $c[$argClass];
64 } else {
65 $ap[] = 0;
66 }
67 }
68 $ret = $ref->newInstanceArgs($ap);
69 return $ret;
70 };
71 $this[$class] = $fn;
72 return null;
73 }
74
75 /**
76 * Overridden to see if the class can be auto-registered and return true if
77 * it can.
78 */
79 public function offsetExists($id) : bool
80 {
81 $exists = parent::offsetExists($id);
82 if (!$exists && \class_exists($id)) {
83 $this->autoRegister($id);
84 return true;
85 }
86 return $exists;
87 }
88
89 /**
90 * Overridden to see if the class can be auto-registered and return an
91 * instance if it can.
92 *
93 * @param string | int $id
94 *
95 * @return mixed The value of the parameter or an object
96 *
97 * @throws UnknownIdentifierException
98 */
99 #[\ReturnTypeWillChange]
100 public function offsetGet($id)
101 {
102 try {
103 return parent::offsetGet($id);
104 } catch (UnknownIdentifierException $e) {
105 if (\class_exists($id)) {
106 $this->autoRegister($id);
107 return parent::offsetGet($id);
108 }
109 throw $e;
110 }
111 }
112
113 /**
114 * Overridden to see if the class can be auto-registered first before
115 * calling Pimple\Container::extend
116 *
117 * @param string $id
118 * @param callable $callable
119 * @return callable the wrapped $callable
120 */
121 public function extend($id, $callable)
122 {
123 $this->offsetExists($id);
124 return parent::extend($id, $callable);
125 }
126 }
127