| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\Lib; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Yoast\WP\Lib\Dependency_Injection\Container_Registry; |
| 7 |
use Yoast\WP\SEO\Loader; |
| 8 |
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Abstract class to extend for the main class in a plugin. |
| 12 |
*/ |
| 13 |
abstract class Abstract_Main { |
| 14 |
|
| 15 |
/** |
| 16 |
* The DI container. |
| 17 |
* |
| 18 |
* @var ContainerInterface|null |
| 19 |
*/ |
| 20 |
protected $container; |
| 21 |
|
| 22 |
/** |
| 23 |
* Loads the plugin. |
| 24 |
* |
| 25 |
* @throws Exception If loading fails and YOAST_ENVIRONMENT is development. |
| 26 |
*/ |
| 27 |
public function load() { |
| 28 |
if ( $this->container ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
try { |
| 33 |
$this->container = $this->get_container(); |
| 34 |
Container_Registry::register( $this->get_name(), $this->container ); |
| 35 |
|
| 36 |
if ( ! $this->container ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
if ( ! $this->container->has( Loader::class ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->container->get( Loader::class )->load(); |
| 44 |
} catch ( Exception $e ) { |
| 45 |
if ( $this->is_development() ) { |
| 46 |
throw $e; |
| 47 |
} |
| 48 |
// Don't crash the entire site, simply don't load. |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Magic getter for retrieving a property. |
| 54 |
* |
| 55 |
* @param string $property The property to retrieve. |
| 56 |
* |
| 57 |
* @return string The value of the property. |
| 58 |
* |
| 59 |
* @throws Exception When the property doesn't exist. |
| 60 |
*/ |
| 61 |
public function __get( $property ) { |
| 62 |
$surfaces = $this->get_surfaces(); |
| 63 |
|
| 64 |
if ( isset( $surfaces[ $property ] ) ) { |
| 65 |
$this->{$property} = $this->container->get( $surfaces[ $property ] ); |
| 66 |
|
| 67 |
return $this->{$property}; |
| 68 |
} |
| 69 |
throw new Exception( "Property $property does not exist." ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Checks if the given property exists as a surface. |
| 74 |
* |
| 75 |
* @param string $property The property to retrieve. |
| 76 |
* |
| 77 |
* @return bool True when property is set. |
| 78 |
*/ |
| 79 |
public function __isset( $property ) { |
| 80 |
return isset( $this->surfaces[ $property ] ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Loads the DI container. |
| 85 |
* |
| 86 |
* @return ContainerInterface|null The DI container. |
| 87 |
* |
| 88 |
* @throws Exception If something goes wrong generating the DI container. |
| 89 |
*/ |
| 90 |
abstract protected function get_container(); |
| 91 |
|
| 92 |
/** |
| 93 |
* Gets the name of the plugin. |
| 94 |
* |
| 95 |
* @return string The name. |
| 96 |
*/ |
| 97 |
abstract protected function get_name(); |
| 98 |
|
| 99 |
/** |
| 100 |
* Gets the surfaces of this plugin. |
| 101 |
* |
| 102 |
* @return array A mapping of surface name to the responsible class. |
| 103 |
*/ |
| 104 |
abstract protected function get_surfaces(); |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns whether or not we're in an environment for Yoast development. |
| 108 |
* |
| 109 |
* @return bool Whether or not to load in development mode. |
| 110 |
*/ |
| 111 |
protected function is_development() { |
| 112 |
try { |
| 113 |
return \WPSEO_Utils::is_development_mode(); |
| 114 |
} |
| 115 |
catch ( \Exception $exception ) { |
| 116 |
// E.g. when WordPress and/or WordPress SEO are not loaded. |
| 117 |
return \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT === 'development'; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|