horizontal
3 days ago
leftboard
3 days ago
abstractlist.php
3 days ago
custom.php
3 days ago
factory.php
3 days ago
index.html
3 days ago
item.php
3 days ago
separator.php
3 days ago
shape.php
3 days ago
factory.php
197 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | VAPLoader::import('libraries.menu.abstractlist'); |
| 15 | |
| 16 | /** |
| 17 | * Abstract factory used to create menu elements. |
| 18 | * |
| 19 | * @since 1.6.3 |
| 20 | */ |
| 21 | abstract class MenuFactory |
| 22 | { |
| 23 | /** |
| 24 | * The default menu type to force. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public static $menuType = null; |
| 29 | |
| 30 | /** |
| 31 | * A list of cached classes. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | protected static $classMap = array(); |
| 36 | |
| 37 | /** |
| 38 | * Returns the current menu type. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public static function getMenuType() |
| 43 | { |
| 44 | // check if the menu type was already fetched |
| 45 | if (is_null(static::$menuType)) |
| 46 | { |
| 47 | /** |
| 48 | * Trigger event to allow the plugins to choose a specific type to |
| 49 | * use for the layout of the back-end menu. |
| 50 | * By default, VikAppointments supports only 2 menu types: |
| 51 | * - 'leftboard', vertical layout placed on the left side; |
| 52 | * - 'horizontal', horizontal layout placed above the component contents. |
| 53 | * |
| 54 | * @return string The menu type to use. |
| 55 | * |
| 56 | * @since 1.6.3 |
| 57 | */ |
| 58 | static::$menuType = VAPFactory::getEventDispatcher()->triggerOnce('onBeforeDefineVikAppointmentsMenuType'); |
| 59 | |
| 60 | // fallback to a default menu type if not specified by a custom plugin |
| 61 | if (!static::$menuType) |
| 62 | { |
| 63 | if (VersionListener::getPlatform() == 'joomla') |
| 64 | { |
| 65 | // use Leftboard Menu in case of Joomla |
| 66 | static::$menuType = 'leftboard'; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // use Horizontal menu in case of WordPress |
| 71 | static::$menuType = 'horizontal'; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return static::$menuType; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Creates a new concrete menu shape instance. |
| 81 | * @see MenuShape for further details about the supported parameters. |
| 82 | * |
| 83 | * @return MenuShape |
| 84 | * |
| 85 | * @uses create() |
| 86 | */ |
| 87 | public static function createMenu() |
| 88 | { |
| 89 | return static::create('shape', 'MenuShape', func_get_args()); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a new concrete menu separator instance. |
| 94 | * @see SeparatorItemShape for further details about the supported parameters. |
| 95 | * |
| 96 | * @return SeparatorItemShape |
| 97 | * |
| 98 | * @uses create() |
| 99 | */ |
| 100 | public static function createSeparator() |
| 101 | { |
| 102 | return static::create('separator', 'SeparatorItemShape', func_get_args()); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates a new concrete menu item instance. |
| 107 | * @see MenuItemShape for further details about the supported parameters. |
| 108 | * |
| 109 | * @return MenuItemShape |
| 110 | * |
| 111 | * @uses create() |
| 112 | */ |
| 113 | public static function createItem() |
| 114 | { |
| 115 | return static::create('item', 'MenuItemShape', func_get_args()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates a new concrete custom menu item instance. |
| 120 | * @see CustomShape for further details about the supported parameters. |
| 121 | * |
| 122 | * @param string $custom The name of the custom item. |
| 123 | * |
| 124 | * @return CustomShape |
| 125 | * |
| 126 | * @uses create() |
| 127 | */ |
| 128 | public static function createCustomItem($custom) |
| 129 | { |
| 130 | // get arguments |
| 131 | $args = func_get_args(); |
| 132 | |
| 133 | // remove custom name from arguments |
| 134 | $custom = array_shift($args); |
| 135 | |
| 136 | return static::create('custom.' . $custom, 'CustomShape', $args); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Creates a new concrete instance. |
| 141 | * |
| 142 | * @param string $alias The alias name ['shape', 'separator', 'item' or 'custom']. |
| 143 | * @param string $parent The parent class ['MenuShape', 'SeparatorItemShape', 'MenuItemShape' or 'CustomShape']. |
| 144 | * @param array $args The class constructor arguments. |
| 145 | * |
| 146 | * @return mixed The instantiated class. |
| 147 | * |
| 148 | * @uses getMenuType() |
| 149 | */ |
| 150 | protected static function create($alias, $parent, array $args = array()) |
| 151 | { |
| 152 | if (!isset(static::$classMap[$alias])) |
| 153 | { |
| 154 | // get menu type |
| 155 | $type = static::getMenuType(); |
| 156 | |
| 157 | // try to load concrete instance |
| 158 | if (!VAPLoader::import('libraries.menu.' . $type . '.' . $alias)) |
| 159 | { |
| 160 | // menu type not found |
| 161 | throw new Exception(sprintf('Impossible to load [%s.%s] menu type', $type, $alias), 404); |
| 162 | } |
| 163 | |
| 164 | // build classname |
| 165 | $classname = ucfirst($type) . ucfirst($parent); |
| 166 | |
| 167 | if (preg_match("/\.(.*?)$/", $alias, $match)) |
| 168 | { |
| 169 | // extract class suffix from alias |
| 170 | $classname .= ucfirst(end($match)); |
| 171 | } |
| 172 | |
| 173 | // make sure the class exists |
| 174 | if (!class_exists($classname)) |
| 175 | { |
| 176 | // the loaded file doesn't contain the class we were looking for |
| 177 | throw new Exception(sprintf('Menu class [%s] not found', $classname), 404); |
| 178 | } |
| 179 | |
| 180 | // make sure the class is a valid instance |
| 181 | if (!is_subclass_of($classname, $parent)) |
| 182 | { |
| 183 | // the class exists but it is not inheriting MenuShape |
| 184 | throw new Exception(sprintf('Menu class [%s] must be an instance of [%s]', $classname, $parent), 500); |
| 185 | } |
| 186 | |
| 187 | // cache class name |
| 188 | static::$classMap[$alias] = $classname; |
| 189 | } |
| 190 | |
| 191 | // create class reflection |
| 192 | $reflect = new ReflectionClass(static::$classMap[$alias]); |
| 193 | // instantiate class by using the specified arguments |
| 194 | return $reflect->newInstanceArgs($args); |
| 195 | } |
| 196 | } |
| 197 |