PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.5
Independent Analytics – WordPress Analytics Plugin v2.15.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / nesbot / carbon / src / Carbon / Traits / Mixin.php
independent-analytics / vendor / nesbot / carbon / src / Carbon / Traits Last commit date
Boundaries.php 2 years ago Cast.php 2 years ago Comparison.php 1 year ago Converter.php 2 years ago Creator.php 2 years ago Date.php 1 month ago DeprecatedProperties.php 2 years ago Difference.php 2 years ago IntervalRounding.php 2 years ago IntervalStep.php 2 years ago Localization.php 1 month ago Macro.php 2 years ago MagicParameter.php 2 years ago Mixin.php 2 years ago Modifiers.php 2 years ago Mutability.php 2 years ago ObjectInitialisation.php 2 years ago Options.php 1 year ago Rounding.php 2 years ago Serialization.php 2 years ago Test.php 1 month ago Timestamp.php 1 month ago ToStringFormat.php 2 years ago Units.php 2 years ago Week.php 2 years ago
Mixin.php
190 lines
1 <?php
2
3 /**
4 * This file is part of the Carbon package.
5 *
6 * (c) Brian Nesbitt <brian@nesbot.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace IAWPSCOPED\Carbon\Traits;
12
13 use IAWPSCOPED\Carbon\CarbonInterface;
14 use IAWPSCOPED\Carbon\CarbonInterval;
15 use IAWPSCOPED\Carbon\CarbonPeriod;
16 use Closure;
17 use Generator;
18 use ReflectionClass;
19 use ReflectionException;
20 use ReflectionMethod;
21 use Throwable;
22 /**
23 * Trait Mixin.
24 *
25 * Allows mixing in entire classes with multiple macros.
26 * @internal
27 */
28 trait Mixin
29 {
30 /**
31 * Stack of macro instance contexts.
32 *
33 * @var array
34 */
35 protected static $macroContextStack = [];
36 /**
37 * Mix another object into the class.
38 *
39 * @example
40 * ```
41 * Carbon::mixin(new class {
42 * public function addMoon() {
43 * return function () {
44 * return $this->addDays(30);
45 * };
46 * }
47 * public function subMoon() {
48 * return function () {
49 * return $this->subDays(30);
50 * };
51 * }
52 * });
53 * $fullMoon = Carbon::create('2018-12-22');
54 * $nextFullMoon = $fullMoon->addMoon();
55 * $blackMoon = Carbon::create('2019-01-06');
56 * $previousBlackMoon = $blackMoon->subMoon();
57 * echo "$nextFullMoon\n";
58 * echo "$previousBlackMoon\n";
59 * ```
60 *
61 * @param object|string $mixin
62 *
63 * @throws ReflectionException
64 *
65 * @return void
66 */
67 public static function mixin($mixin)
68 {
69 \is_string($mixin) && \trait_exists($mixin) ? self::loadMixinTrait($mixin) : self::loadMixinClass($mixin);
70 }
71 /**
72 * @param object|string $mixin
73 *
74 * @throws ReflectionException
75 */
76 private static function loadMixinClass($mixin)
77 {
78 $methods = (new ReflectionClass($mixin))->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED);
79 foreach ($methods as $method) {
80 if ($method->isConstructor() || $method->isDestructor()) {
81 continue;
82 }
83 $method->setAccessible(\true);
84 static::macro($method->name, $method->invoke($mixin));
85 }
86 }
87 /**
88 * @param string $trait
89 */
90 private static function loadMixinTrait($trait)
91 {
92 $context = eval(self::getAnonymousClassCodeForTrait($trait));
93 $className = \get_class($context);
94 $baseClass = static::class;
95 foreach (self::getMixableMethods($context) as $name) {
96 $closureBase = Closure::fromCallable([$context, $name]);
97 static::macro($name, function (...$parameters) use($closureBase, $className, $baseClass) {
98 $downContext = isset($this) ? $this : new $baseClass();
99 $context = isset($this) ? $this->cast($className) : new $className();
100 try {
101 // @ is required to handle error if not converted into exceptions
102 $closure = @$closureBase->bindTo($context);
103 } catch (Throwable $throwable) {
104 // @codeCoverageIgnore
105 $closure = $closureBase;
106 // @codeCoverageIgnore
107 }
108 // in case of errors not converted into exceptions
109 $closure = $closure ?: $closureBase;
110 $result = $closure(...$parameters);
111 if (!$result instanceof $className) {
112 return $result;
113 }
114 if ($downContext instanceof CarbonInterface && $result instanceof CarbonInterface) {
115 if ($context !== $result) {
116 $downContext = $downContext->copy();
117 }
118 return $downContext->setTimezone($result->getTimezone())->modify($result->format('Y-m-d H:i:s.u'))->settings($result->getSettings());
119 }
120 if ($downContext instanceof CarbonInterval && $result instanceof CarbonInterval) {
121 if ($context !== $result) {
122 $downContext = $downContext->copy();
123 }
124 $downContext->copyProperties($result);
125 self::copyStep($downContext, $result);
126 self::copyNegativeUnits($downContext, $result);
127 return $downContext->settings($result->getSettings());
128 }
129 if ($downContext instanceof CarbonPeriod && $result instanceof CarbonPeriod) {
130 if ($context !== $result) {
131 $downContext = $downContext->copy();
132 }
133 return $downContext->setDates($result->getStartDate(), $result->getEndDate())->setRecurrences($result->getRecurrences())->setOptions($result->getOptions())->settings($result->getSettings());
134 }
135 return $result;
136 });
137 }
138 }
139 private static function getAnonymousClassCodeForTrait(string $trait)
140 {
141 return 'return new class() extends ' . static::class . ' {use ' . $trait . ';};';
142 }
143 private static function getMixableMethods(self $context) : Generator
144 {
145 foreach (\get_class_methods($context) as $name) {
146 if (\method_exists(static::class, $name)) {
147 continue;
148 }
149 (yield $name);
150 }
151 }
152 /**
153 * Stack a Carbon context from inside calls of self::this() and execute a given action.
154 *
155 * @param static|null $context
156 * @param callable $callable
157 *
158 * @throws Throwable
159 *
160 * @return mixed
161 */
162 protected static function bindMacroContext($context, callable $callable)
163 {
164 static::$macroContextStack[] = $context;
165 try {
166 return $callable();
167 } finally {
168 \array_pop(static::$macroContextStack);
169 }
170 }
171 /**
172 * Return the current context from inside a macro callee or a null if static.
173 *
174 * @return static|null
175 */
176 protected static function context()
177 {
178 return \end(static::$macroContextStack) ?: null;
179 }
180 /**
181 * Return the current context from inside a macro callee or a new one if static.
182 *
183 * @return static
184 */
185 protected static function this()
186 {
187 return \end(static::$macroContextStack) ?: new static();
188 }
189 }
190