← All changes
|
app/Services/ShortCodeParser/Parsers/BaseParser.php
+14
-19
1.3.20
→
1.6.5
View file →
| @@ -16,19 +16,14 @@ | ||
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * @var array $instances |
| 19 | 19 | * |
| 20 | - * This static property is used to cache the results of method and attribute lookups. | |
| 21 | - * | |
| 22 | - * The `BaseParser` class uses this array to store the values retrieved by the `get` method. | |
| 23 | - * When a value is requested for a specific code, the `get` method first checks if the value | |
| 24 | - * is already cached in this array. If it is, the cached value is returned, avoiding redundant | |
| 25 | - * lookups or method calls. | |
| 26 | - * | |
| 27 | - * This caching mechanism improves performance by reducing the number of times the same | |
| 28 | - * data needs to be retrieved or computed. | |
| 20 | + * Per-instance cache of method and attribute lookups for the record this parser | |
| 21 | + * was constructed with. Must stay instance-scoped, not static — a static cache | |
| 22 | + * keyed only by accessor bleeds values across different records parsed in the | |
| 23 | + * same request (e.g. two orders processed by one cron run). | |
| 29 | 24 | */ |
| 30 | - protected static array $instances = []; | |
| 25 | + protected array $instances = []; | |
| 31 | 26 | |
| 32 | 27 | /** |
| 33 | 28 | * @var array $methodMap |
| 34 | 29 | * |
| @@ -76,9 +71,9 @@ | ||
| 76 | 71 | |
| 77 | 72 | /** |
| 78 | 73 | * Retrieves the value for the given code. |
| 79 | 74 | * |
| 80 | - * This method first checks if the value for the given code is already cached in the `static::$instances` array. | |
| 75 | + * This method first checks if the value for the given code is already cached in the `$this->instances` array. | |
| 81 | 76 | * If it is, the cached value is returned. If not, it checks if the code corresponds to a method or an attribute. |
| 82 | 77 | * If the code corresponds to a method, the `getByMethod` method is called to retrieve the value. |
| 83 | 78 | * If the code corresponds to an attribute, the `getAttribute` method is called to retrieve the value. |
| 84 | 79 | * If the code does not correspond to any method or attribute, a placeholder string with the code is returned. |
| @@ -96,10 +91,10 @@ | ||
| 96 | 91 | //update the template after parsing $conditions |
| 97 | 92 | //order.payment_method||title_case -> to order.payment_method |
| 98 | 93 | $template = $conditions['accessor']; |
| 99 | 94 | |
| 100 | - if (isset(static::$instances[$accessor])) { | |
| 101 | - return static::$instances[$accessor]; | |
| 95 | + if (isset($this->instances[$accessor])) { | |
| 96 | + return $this->instances[$accessor]; | |
| 102 | 97 | } |
| 103 | 98 | |
| 104 | 99 | if (isset($this->methodMap[$accessor])) { |
| 105 | 100 | return $this->getByMethod($accessor, $template, $conditions); |
| @@ -122,9 +117,9 @@ | ||
| 122 | 117 | /** |
| 123 | 118 | * Retrieves the value for the given code from the data array. |
| 124 | 119 | * |
| 125 | 120 | * This method uses the `attributeMap` array to find the corresponding key in the data array |
| 126 | - * and retrieves its value. The result is then stored in the `static::$instances` array | |
| 121 | + * and retrieves its value. The result is then stored in the `$this->instances` array | |
| 127 | 122 | * to avoid redundant lookups in the future. |
| 128 | 123 | * |
| 129 | 124 | * @param string $code The code representing the attribute to be retrieved. |
| 130 | 125 | * @return string The value of the attribute associated with the given code. |
| @@ -130,10 +125,10 @@ | ||
| 130 | 125 | * @return string The value of the attribute associated with the given code. |
| 131 | 126 | */ |
| 132 | 127 | protected function getAttribute(string $code): string |
| 133 | 128 | { |
| 134 | - static::$instances[$code] = Arr::get($this->data, $this->attributeMap[$code]); | |
| 135 | - return static::$instances[$code]; | |
| 129 | + $this->instances[$code] = Arr::get($this->data, $this->attributeMap[$code]); | |
| 130 | + return $this->instances[$code]; | |
| 136 | 131 | } |
| 137 | 132 | |
| 138 | 133 | /** |
| 139 | 134 | * Retrieves the value for the given code by invoking the corresponding method. |
| @@ -138,9 +133,9 @@ | ||
| 138 | 133 | /** |
| 139 | 134 | * Retrieves the value for the given code by invoking the corresponding method. |
| 140 | 135 | * |
| 141 | 136 | * This method checks the `methodMap` array for the provided code and calls the |
| 142 | - * associated method. The result is then stored in the `static::$instances` array | |
| 137 | + * associated method. The result is then stored in the `$this->instances` array | |
| 143 | 138 | * to avoid redundant method calls in the future. |
| 144 | 139 | * |
| 145 | 140 | * @param string $accessor The code representing the method to be called. |
| 146 | 141 | * @param ?string $template The full shortcode. |
| @@ -147,8 +142,8 @@ | ||
| 147 | 142 | * @return string The result of the method call associated with the given code. |
| 148 | 143 | */ |
| 149 | 144 | protected function getByMethod(string $accessor, ?string $template, $conditions = []): ?string |
| 150 | 145 | { |
| 151 | - static::$instances[$accessor] = $this->{$this->methodMap[$accessor]}($accessor, $template, $conditions); | |
| 152 | - return static::$instances[$accessor]; | |
| 146 | + $this->instances[$accessor] = $this->{$this->methodMap[$accessor]}($accessor, $template, $conditions); | |
| 147 | + return $this->instances[$accessor]; | |
| 153 | 148 | } |
| 154 | 149 | } |