ServiceProvider.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\LegacySubscriptions; |
| 4 | |
| 5 | use Closure; |
| 6 | use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 7 | |
| 8 | /** |
| 9 | * Class ServiceProvider - LegacySubscriptions |
| 10 | * |
| 11 | * This handles the loading of all the legacy codebase included in the LegacySubscriptions /includes directory. |
| 12 | * DO NOT EXTEND THIS WITH NEW CODE as it is intended to shrink over time as we migrate over |
| 13 | * to the new ways of doing things. |
| 14 | * |
| 15 | * @since 2.19.0 |
| 16 | */ |
| 17 | class ServiceProvider implements ServiceProviderInterface |
| 18 | { |
| 19 | /** |
| 20 | * @inheritDoc |
| 21 | */ |
| 22 | public function register() |
| 23 | { |
| 24 | $recurringIsInstalled = defined('GIVE_RECURRING_VERSION') && GIVE_RECURRING_VERSION; |
| 25 | $recurringMeetsRequirements = $recurringIsInstalled && version_compare(GIVE_RECURRING_VERSION, '1.14.1', '>'); |
| 26 | |
| 27 | if ($recurringMeetsRequirements || !$recurringIsInstalled) { |
| 28 | $this->includeLegacyFiles(); |
| 29 | $this->bindClasses(); |
| 30 | } |
| 31 | |
| 32 | $this->includeLegacyHelpers(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @inheritDoc |
| 37 | */ |
| 38 | public function boot() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Load all the legacy class files since they don't have autoloading |
| 44 | * |
| 45 | * @since 2.19.0 |
| 46 | */ |
| 47 | private function includeLegacyFiles() |
| 48 | { |
| 49 | require_once __DIR__ . '/includes/give-subscriptions-db.php'; |
| 50 | require_once __DIR__ . '/includes/give-recurring-db-subscription-meta.php'; |
| 51 | require_once __DIR__ . '/includes/give-recurring-cache.php'; |
| 52 | require_once __DIR__ . '/includes/give-subscription.php'; |
| 53 | require_once __DIR__ . '/includes/give-subscriptions-api.php'; |
| 54 | require_once __DIR__ . '/includes/give-recurring-subscriber.php'; |
| 55 | require_once __DIR__ . '/includes/give-recurring-cron.php'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Load all the legacy helpers |
| 60 | * |
| 61 | * @since 2.19.0 |
| 62 | */ |
| 63 | private function includeLegacyHelpers() |
| 64 | { |
| 65 | require_once __DIR__ . '/includes/give-recurring-helpers.php'; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Binds the legacy classes to the service provider |
| 70 | * |
| 71 | * @since 2.19.0 |
| 72 | */ |
| 73 | private function bindClasses() |
| 74 | { |
| 75 | $this->bindInstance( |
| 76 | 'subscription_meta', |
| 77 | 'Give_Recurring_DB_Subscription_Meta', |
| 78 | 'give-recurring-db-subscription-meta.php' |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * A helper for loading legacy classes that do not use autoloading, then binding their instance |
| 84 | * to the container. |
| 85 | * |
| 86 | * @since 2.19.0 |
| 87 | * |
| 88 | * @param string $alias |
| 89 | * @param string|Closure $class |
| 90 | * @param string $includesPath |
| 91 | * @param bool $singleton |
| 92 | */ |
| 93 | private function bindInstance($alias, $class, $includesPath, $singleton = false) |
| 94 | { |
| 95 | require_once __DIR__ . "/includes/$includesPath"; |
| 96 | |
| 97 | if ($class instanceof Closure) { |
| 98 | give()->instance($alias, $class()); |
| 99 | } elseif ($singleton) { |
| 100 | give()->instance($alias, $class::get_instance()); |
| 101 | } else { |
| 102 | give()->instance($alias, new $class()); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 |