| @@ -2,9 +2,11 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\Lib; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | +use WPSEO_Utils; | |
| 6 | 7 | use Yoast\WP\Lib\Dependency_Injection\Container_Registry; |
| 8 | +use Yoast\WP\SEO\Exceptions\Forbidden_Property_Mutation_Exception; | |
| 7 | 9 | use Yoast\WP\SEO\Loader; |
| 8 | 10 | use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface; |
| 9 | 11 | |
| 10 | 12 | /** |
| @@ -19,10 +21,19 @@ | ||
| 19 | 21 | */ |
| 20 | 22 | protected $container; |
| 21 | 23 | |
| 22 | 24 | /** |
| 25 | + * A cache for previously requested and constructed surfaces. | |
| 26 | + * | |
| 27 | + * @var mixed[] | |
| 28 | + */ | |
| 29 | + private $cached_surfaces = []; | |
| 30 | + | |
| 31 | + /** | |
| 23 | 32 | * Loads the plugin. |
| 24 | 33 | * |
| 34 | + * @return void | |
| 35 | + * | |
| 25 | 36 | * @throws Exception If loading fails and YOAST_ENVIRONMENT is development. |
| 26 | 37 | */ |
| 27 | 38 | public function load() { |
| 28 | 39 | if ( $this->container ) { |
| @@ -49,25 +60,29 @@ | ||
| 49 | 60 | } |
| 50 | 61 | } |
| 51 | 62 | |
| 52 | 63 | /** |
| 53 | - * Magic getter for retrieving a property. | |
| 64 | + * Magic getter for retrieving a property from a surface. | |
| 54 | 65 | * |
| 55 | 66 | * @param string $property The property to retrieve. |
| 56 | 67 | * |
| 57 | - * @return string The value of the property. | |
| 68 | + * @return mixed The value of the property. | |
| 58 | 69 | * |
| 59 | 70 | * @throws Exception When the property doesn't exist. |
| 60 | 71 | */ |
| 61 | 72 | public function __get( $property ) { |
| 73 | + if ( \array_key_exists( $property, $this->cached_surfaces ) ) { | |
| 74 | + return $this->cached_surfaces[ $property ]; | |
| 75 | + } | |
| 76 | + | |
| 62 | 77 | $surfaces = $this->get_surfaces(); |
| 63 | 78 | |
| 64 | 79 | if ( isset( $surfaces[ $property ] ) ) { |
| 65 | - $this->{$property} = $this->container->get( $surfaces[ $property ] ); | |
| 80 | + $this->cached_surfaces[ $property ] = $this->container->get( $surfaces[ $property ] ); | |
| 66 | 81 | |
| 67 | - return $this->{$property}; | |
| 82 | + return $this->cached_surfaces[ $property ]; | |
| 68 | 83 | } |
| 69 | - throw new Exception( "Property $property does not exist." ); | |
| 84 | + throw new Exception( \sprintf( 'Property $%s does not exist.', $property ) ); | |
| 70 | 85 | } |
| 71 | 86 | |
| 72 | 87 | /** |
| 73 | 88 | * Checks if the given property exists as a surface. |
| @@ -76,12 +91,51 @@ | ||
| 76 | 91 | * |
| 77 | 92 | * @return bool True when property is set. |
| 78 | 93 | */ |
| 79 | 94 | public function __isset( $property ) { |
| 80 | - return isset( $this->surfaces[ $property ] ); | |
| 95 | + if ( \array_key_exists( $property, $this->cached_surfaces ) ) { | |
| 96 | + return true; | |
| 97 | + } | |
| 98 | + | |
| 99 | + $surfaces = $this->get_surfaces(); | |
| 100 | + | |
| 101 | + if ( ! isset( $surfaces[ $property ] ) ) { | |
| 102 | + return false; | |
| 103 | + } | |
| 104 | + | |
| 105 | + return $this->container->has( $surfaces[ $property ] ); | |
| 81 | 106 | } |
| 82 | 107 | |
| 83 | 108 | /** |
| 109 | + * Prevents setting dynamic properties and unsetting declared properties | |
| 110 | + * from an inaccessible context. | |
| 111 | + * | |
| 112 | + * @param string $name The property name. | |
| 113 | + * @param mixed $value The property value. | |
| 114 | + * | |
| 115 | + * @return void | |
| 116 | + * | |
| 117 | + * @throws Forbidden_Property_Mutation_Exception Set is never meant to be called. | |
| 118 | + */ | |
| 119 | + public function __set( $name, $value ) { // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- __set must have a name and value - PHPCS #3715. | |
| 120 | + throw Forbidden_Property_Mutation_Exception::cannot_set_because_property_is_immutable( $name ); | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Prevents unsetting dynamic properties and unsetting declared properties | |
| 125 | + * from an inaccessible context. | |
| 126 | + * | |
| 127 | + * @param string $name The property name. | |
| 128 | + * | |
| 129 | + * @return void | |
| 130 | + * | |
| 131 | + * @throws Forbidden_Property_Mutation_Exception Unset is never meant to be called. | |
| 132 | + */ | |
| 133 | + public function __unset( $name ) { | |
| 134 | + throw Forbidden_Property_Mutation_Exception::cannot_unset_because_property_is_immutable( $name ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 84 | 138 | * Loads the DI container. |
| 85 | 139 | * |
| 86 | 140 | * @return ContainerInterface|null The DI container. |
| 87 | 141 | * |
| @@ -109,11 +163,10 @@ | ||
| 109 | 163 | * @return bool Whether or not to load in development mode. |
| 110 | 164 | */ |
| 111 | 165 | protected function is_development() { |
| 112 | 166 | try { |
| 113 | - return \WPSEO_Utils::is_development_mode(); | |
| 114 | - } | |
| 115 | - catch ( \Exception $exception ) { | |
| 167 | + return WPSEO_Utils::is_development_mode(); | |
| 168 | + } catch ( Exception $exception ) { | |
| 116 | 169 | // E.g. when WordPress and/or WordPress SEO are not loaded. |
| 117 | 170 | return \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT === 'development'; |
| 118 | 171 | } |
| 119 | 172 | } |