abstract-ad.php
2 days ago
abstract-admin-list-table.php
1 week ago
abstract-data.php
1 week ago
abstract-factory.php
2 days ago
abstract-group.php
2 days ago
abstract-placement-type.php
1 week ago
abstract-placement.php
2 days ago
abstract-screen.php
3 months ago
abstract-types.php
2 days ago
index.php
2 years ago
abstract-factory.php
105 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abstracts Factory. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.47.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Abstracts; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Abstracts Factory. |
| 16 | */ |
| 17 | abstract class Factory { |
| 18 | |
| 19 | /** |
| 20 | * Request-scoped cache of loaded entity instances keyed by entity ID and optional type override. |
| 21 | * |
| 22 | * @var array<string, object|false> |
| 23 | */ |
| 24 | protected $instances = []; |
| 25 | |
| 26 | /** |
| 27 | * Clear request-scoped entity instances (e.g. after CRUD writes). |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function clear_instance_cache(): void { |
| 32 | $this->instances = []; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Build a cache key for a loaded entity instance. |
| 37 | * |
| 38 | * @param int $id Entity ID. |
| 39 | * @param string $new_type Optional type override passed to the factory getter. |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | protected function get_instance_cache_key( int $id, string $new_type = '' ): string { |
| 44 | return '' !== $new_type ? $id . ':' . $new_type : (string) $id; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Whether a cached entity instance exists for the given ID and type override. |
| 49 | * |
| 50 | * @param int $id Entity ID. |
| 51 | * @param string $new_type Optional type override passed to the factory getter. |
| 52 | * |
| 53 | * @return bool |
| 54 | */ |
| 55 | protected function has_cached_instance( int $id, string $new_type = '' ): bool { |
| 56 | return array_key_exists( $this->get_instance_cache_key( $id, $new_type ), $this->instances ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get a cached entity instance for the given ID and type override. |
| 61 | * |
| 62 | * @param int $id Entity ID. |
| 63 | * @param string $new_type Optional type override passed to the factory getter. |
| 64 | * |
| 65 | * @return object|false |
| 66 | */ |
| 67 | protected function get_cached_instance( int $id, string $new_type = '' ) { |
| 68 | return $this->instances[ $this->get_instance_cache_key( $id, $new_type ) ]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Store an entity instance in the request-scoped cache. |
| 73 | * |
| 74 | * @param int $id Entity ID. |
| 75 | * @param string $new_type Optional type override passed to the factory getter. |
| 76 | * @param object|false $instance Loaded entity instance. |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | protected function store_cached_instance( int $id, string $new_type, $instance ): void { |
| 81 | $this->instances[ $this->get_instance_cache_key( $id, $new_type ) ] = $instance; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Create an empty entity from a registered type object. |
| 86 | * |
| 87 | * @param object|false $type_object Registered type object. |
| 88 | * @param int $id Entity ID. |
| 89 | * |
| 90 | * @return object|false |
| 91 | */ |
| 92 | protected function create_empty_from_type( $type_object, int $id = 0 ) { |
| 93 | if ( ! $type_object ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | $classname = $type_object->get_classname(); |
| 98 | $entity = new $classname( $id ); |
| 99 | $entity->set_type( $type_object->get_id() ); |
| 100 | |
| 101 | return $entity; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |