router.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.application |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 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 | /** |
| 15 | * Class to create and parse routes. |
| 16 | * |
| 17 | * @since 10.1.19 |
| 18 | */ |
| 19 | abstract class JRouter |
| 20 | { |
| 21 | /** |
| 22 | * Router instances container. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | protected static $instances = array(); |
| 27 | |
| 28 | /** |
| 29 | * A configuration array. |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | protected $options; |
| 34 | |
| 35 | /** |
| 36 | * Class constructor. |
| 37 | * |
| 38 | * @param array $options Array of options. |
| 39 | */ |
| 40 | public function __construct($options = array()) |
| 41 | { |
| 42 | $this->options = (array) $options; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the global Router object, only creating it if it |
| 47 | * doesn't already exist. |
| 48 | * |
| 49 | * @param string $client The name of the client. |
| 50 | * @param array $options An associative array of options. |
| 51 | * |
| 52 | * @return Router A Router object. |
| 53 | * |
| 54 | * @throws Exception |
| 55 | */ |
| 56 | public static function getInstance($client, $options = array()) |
| 57 | { |
| 58 | $client = strtolower($client); |
| 59 | |
| 60 | if (empty(self::$instances[$client])) |
| 61 | { |
| 62 | // try to search for a router within the plugin folder |
| 63 | if (!JLoader::import($client . '.router', WP_PLUGIN_DIR)) |
| 64 | { |
| 65 | // try to load a native file |
| 66 | if (!JLoader::import('adapter.router.classes.' . $client)) |
| 67 | { |
| 68 | throw new Exception(sprintf('Router [%s] not found', $client), 404); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // create a Menu object |
| 73 | $classname = 'JRouter' . ucfirst($client); |
| 74 | |
| 75 | // make sure the class exists and it is valid |
| 76 | if (!class_exists($classname) || !is_subclass_of($classname, 'JRouter')) |
| 77 | { |
| 78 | throw new Exception(sprintf('Invalid router [%s] class', $classname), 500); |
| 79 | } |
| 80 | |
| 81 | // instantiate the class and cache it |
| 82 | self::$instances[$client] = new $classname($options); |
| 83 | } |
| 84 | |
| 85 | return self::$instances[$client]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Function to convert an internal URI to a route. |
| 90 | * |
| 91 | * @param mixed $url The internal URL or an associative array. |
| 92 | * |
| 93 | * @return mixed The absolute search engine friendly URL object. |
| 94 | */ |
| 95 | abstract public function build($url); |
| 96 | } |
| 97 |