Blocks
1 week ago
Contracts
2 years ago
Database
1 week ago
Integrations
3 weeks ago
Libraries
2 months ago
Models
1 week ago
Seeds
1 week ago
Services
1 week ago
Support
2 months ago
config
1 week ago
lib
3 months ago
Activator.php
1 week ago
Attachment.php
2 months ago
Controller.php
2 years ago
Core.php
2 years ago
Deactivator.php
1 week ago
Factory.php
1 week ago
Files.php
2 months ago
Playlist.php
2 years ago
Plugin.php
3 months ago
Requirements.php
2 years ago
support.php
2 months ago
Factory.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * DI container factory and rules. |
| 4 | * |
| 5 | * @package PrestoPlayer |
| 6 | */ |
| 7 | |
| 8 | namespace PrestoPlayer; |
| 9 | |
| 10 | use PrestoPlayer\Plugin; |
| 11 | use PrestoPlayer\Attachment; |
| 12 | use PrestoPlayer\Controller; |
| 13 | use PrestoPlayer\Support\Block; |
| 14 | use PrestoPlayer\Services\Scripts; |
| 15 | // Disabled import: PrestoPlayer\Services\BunnyCDN class does not exist. |
| 16 | // Reference only: use PrestoPlayer\Services\BunnyCDN. |
| 17 | use PrestoPlayer\Services\Settings; |
| 18 | use PrestoPlayer\Services\AdminNotices; |
| 19 | use PrestoPlayer\Services\Usage; |
| 20 | |
| 21 | /** |
| 22 | * Builds the DICE container rules and the plugin component list. |
| 23 | */ |
| 24 | class Factory { |
| 25 | |
| 26 | const SHARED = array( 'shared' => true ); |
| 27 | |
| 28 | /** |
| 29 | * DICE container instance (provides the INSTANCE marker). |
| 30 | * |
| 31 | * @var mixed |
| 32 | */ |
| 33 | public $instance; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | * |
| 38 | * @param mixed $instance DICE container instance. |
| 39 | */ |
| 40 | public function __construct( $instance ) { |
| 41 | $this->instance = $instance; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Whether the Pro plugin is active. |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | public function isPro() { |
| 50 | return Plugin::isPro(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Retrieves the rules for setting up the plugin. |
| 55 | * |
| 56 | * @since 2.1.0 |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public function getRules() { |
| 61 | return array( |
| 62 | Visits::class => self::SHARED, |
| 63 | ReusableVideos::class => self::SHARED, |
| 64 | AdminNotices::class => self::SHARED, |
| 65 | Usage::class => self::SHARED, |
| 66 | |
| 67 | Settings::class => array( |
| 68 | 'constructParams' => array( |
| 69 | $this->isPro(), |
| 70 | ), |
| 71 | ), |
| 72 | |
| 73 | Attachment::class => array( |
| 74 | 'constructParams' => array( |
| 75 | $this->isPro(), |
| 76 | ), |
| 77 | ), |
| 78 | |
| 79 | // Blocks. |
| 80 | Block::class => array( |
| 81 | 'constructParams' => array( |
| 82 | $this->isPro(), |
| 83 | $this->getPluginVersion( PRESTO_PLAYER_PLUGIN_FILE ), |
| 84 | ), |
| 85 | ), |
| 86 | |
| 87 | // Plugin controller. |
| 88 | Controller::class => array( |
| 89 | 'constructParams' => array( $this->getComponents() ), |
| 90 | ), |
| 91 | |
| 92 | Scripts::class => array( |
| 93 | 'shared' => true, |
| 94 | 'constructParams' => array( |
| 95 | $this->isPro(), |
| 96 | $this->getPluginVersion( PRESTO_PLAYER_PLUGIN_FILE ), |
| 97 | ), |
| 98 | ), |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Retrieves the plugin version. |
| 104 | * |
| 105 | * @param string $plugin_file The full plugin path. |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | protected function getPluginVersion( $plugin_file ) { |
| 110 | // Load version from plugin data. |
| 111 | if ( ! \function_exists( 'get_plugin_data' ) ) { |
| 112 | require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 113 | } |
| 114 | |
| 115 | return \get_plugin_data( $plugin_file, false, false )['Version']; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Retrieves the list of plugin components run during normal operations |
| 120 | * (i.e. not including the Uninstallation component). |
| 121 | */ |
| 122 | public function getComponents() { |
| 123 | $config = require_once 'config/app.php'; |
| 124 | $components = $config['components']; |
| 125 | $components = array_merge( $components, $config['pro_components'] ); |
| 126 | |
| 127 | return $this->formatComponents( $components ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Formats components to use in DICE. |
| 132 | * |
| 133 | * @param array $components Component class names. |
| 134 | * @return array |
| 135 | */ |
| 136 | public function formatComponents( $components = array() ) { |
| 137 | $formatted = array(); |
| 138 | |
| 139 | if ( ! $components ) { |
| 140 | return array(); |
| 141 | } |
| 142 | |
| 143 | foreach ( array_filter( $components ) as $component ) { |
| 144 | $formatted[] = array( $this->instance::INSTANCE => $component ); |
| 145 | } |
| 146 | return $formatted; |
| 147 | } |
| 148 | } |
| 149 |