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 |