Contracts
3 years ago
DonationHistoryTab
4 years ago
EditProfileTab
3 years ago
TabsRegister.php
4 years ago
TabsRegister.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Tabs; |
| 4 | |
| 5 | use Give\DonorDashboards\Exceptions\DuplicateTabException; |
| 6 | use Give\DonorDashboards\Exceptions\MissingTabException; |
| 7 | use Give\DonorDashboards\Tabs\Contracts\Tab; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.10.0 |
| 11 | */ |
| 12 | class TabsRegister |
| 13 | { |
| 14 | /** |
| 15 | * FQCN of Tab classes |
| 16 | * |
| 17 | * @since 2.10.0 |
| 18 | * |
| 19 | * @var string[] |
| 20 | */ |
| 21 | private $tabs = []; |
| 22 | |
| 23 | /** |
| 24 | * Returns all of the registered tabs |
| 25 | * |
| 26 | * @since 2.10.0 |
| 27 | * |
| 28 | * @return string[] |
| 29 | */ |
| 30 | public function getTabs() |
| 31 | { |
| 32 | return $this->tabs; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Checks to see if a tab is registered with the given ID |
| 37 | * |
| 38 | * @since 2.10.0 |
| 39 | * |
| 40 | * @param string $id |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function hasTab($id) |
| 45 | { |
| 46 | return isset($this->tabs[$id]); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns a tab with the given ID |
| 51 | * |
| 52 | * @since 2.10.0 |
| 53 | * |
| 54 | * @param string $id |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | public function getTab($id) |
| 59 | { |
| 60 | if ( ! $this->hasTab($id)) { |
| 61 | throw new MissingTabException($id); |
| 62 | } |
| 63 | |
| 64 | return $this->tabs[$id]; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns all of the registered tab ids |
| 69 | * |
| 70 | * @since 2.10.0 |
| 71 | * |
| 72 | * @return string[] |
| 73 | */ |
| 74 | public function getRegisteredIds() |
| 75 | { |
| 76 | return array_keys($this->tabs); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add a tab to the list of tabs |
| 81 | * |
| 82 | * @since 2.10.0 |
| 83 | * |
| 84 | * @param string $tabClass FQCN of the Tab Class |
| 85 | */ |
| 86 | public function addTab($tabClass) |
| 87 | { |
| 88 | if ( ! is_subclass_of($tabClass, Tab::class)) { |
| 89 | throw new \InvalidArgumentException('Class must extend the ' . Tab::class . ' class'); |
| 90 | } |
| 91 | |
| 92 | $tabId = $tabClass::id(); |
| 93 | |
| 94 | if ($this->hasTab($tabId)) { |
| 95 | throw new DuplicateTabException(); |
| 96 | } |
| 97 | |
| 98 | $this->tabs[$tabId] = $tabClass; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Helper for adding a bunch of tabs at once |
| 103 | * |
| 104 | * @since 2.10.0 |
| 105 | * |
| 106 | * @param string[] $tabClasses |
| 107 | */ |
| 108 | public function addTabs(array $tabClasses) |
| 109 | { |
| 110 | foreach ($tabClasses as $tabClass) { |
| 111 | $this->addTab($tabClass); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public function registerTabRoutes() |
| 116 | { |
| 117 | foreach (give()->donorDashboardTabs->tabs as $tabClass) { |
| 118 | $tab = new $tabClass; |
| 119 | $tab->registerRoutes(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public function enqueueTabAssets() |
| 124 | { |
| 125 | foreach (give()->donorDashboardTabs->tabs as $tabClass) { |
| 126 | (new $tabClass)->enqueueAssets(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 |