ServiceTrait.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Reusable elements for each service |
| 12 | * |
| 13 | * @since 6.7.9 https://github.com/aamplugin/advanced-access-manager/issues/193 |
| 14 | * @since 6.4.0 Enhancement https://github.com/aamplugin/advanced-access-manager/issues/71 |
| 15 | * @since 6.0.0 Initial implementation of the service |
| 16 | * |
| 17 | * @package AAM |
| 18 | * @version 6.7.9 |
| 19 | */ |
| 20 | trait AAM_Core_Contract_ServiceTrait |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * Single instance of itself |
| 25 | * |
| 26 | * @var object |
| 27 | * |
| 28 | * @access protected |
| 29 | * @version 6.0.0 |
| 30 | */ |
| 31 | protected static $instance = null; |
| 32 | |
| 33 | /** |
| 34 | * Register service to be fetched |
| 35 | * |
| 36 | * @return null|object |
| 37 | * |
| 38 | * @access protected |
| 39 | * @version 6.4.0 |
| 40 | */ |
| 41 | protected function registerService() |
| 42 | { |
| 43 | add_filter('aam_get_service_filter', function($service, $alias) { |
| 44 | if (empty($service) && ($alias === self::SERVICE_ALIAS)) { |
| 45 | $service = $this; |
| 46 | } |
| 47 | |
| 48 | return $service; |
| 49 | }, 10, 2); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Bootstrap the service |
| 54 | * |
| 55 | * @return void |
| 56 | * |
| 57 | * @param boolean $reload |
| 58 | * |
| 59 | * @since 6.7.9 https://github.com/aamplugin/advanced-access-manager/issues/193 |
| 60 | * @since 6.0.0 Initial implementation of the method |
| 61 | * |
| 62 | * @access public |
| 63 | * @version 6.7.9 |
| 64 | */ |
| 65 | public static function bootstrap($reload = false) |
| 66 | { |
| 67 | if (is_null(self::$instance) || $reload) { |
| 68 | self::$instance = new self; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get single instance of itself |
| 74 | * |
| 75 | * @return object |
| 76 | * |
| 77 | * @param boolean $reload |
| 78 | * |
| 79 | * @since 6.7.9 https://github.com/aamplugin/advanced-access-manager/issues/193 |
| 80 | * @since 6.0.0 Initial implementation of the method |
| 81 | * |
| 82 | * @access public |
| 83 | * @version 6.7.9 |
| 84 | */ |
| 85 | public static function getInstance($reload = false) |
| 86 | { |
| 87 | if (is_null(self::$instance) || $reload) { |
| 88 | self::bootstrap($reload); |
| 89 | } |
| 90 | |
| 91 | return self::$instance; |
| 92 | } |
| 93 | |
| 94 | } |