This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Admin_Page_Collection interface.
- Loading branch information
Felix Arntz
committed
Jan 14, 2018
1 parent
255b4d4
commit 84b6d68
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Admin_Page_Collection interface | ||
* | ||
* @package Leaves_And_Love\OOP_Admin_Pages | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace Leaves_And_Love\OOP_Admin_Pages; | ||
|
||
use Leaves_And_Love\OOP_Admin_Pages\Exception\Admin_Page_Not_Found_Exception; | ||
|
||
/** | ||
* Interface to manage admin page instances. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
interface Admin_Page_Collection { | ||
|
||
/** | ||
* Registers the admin pages in the collection within the environment. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public function register(); | ||
|
||
/** | ||
* Checks whether an admin page is part of the collection. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param string $slug Identifier of the admin page to check for. | ||
* @return bool True if the admin page is part of the collection, false otherwise. | ||
*/ | ||
public function has_page( string $slug ) : bool; | ||
|
||
/** | ||
* Gets an admin page instance that is part of the collection. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param string $slug Identifier of the admin page to get. | ||
* @return Admin_Page Admin page instance. | ||
* | ||
* @throws Admin_Page_Not_Found_Exception Thrown when the admin page is not found. | ||
*/ | ||
public function get_page( string $slug ) : Admin_Page; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* Admin_Page_Not_Found_Exception class | ||
* | ||
* @package Leaves_And_Love\OOP_Admin_Pages\Exception | ||
* @since 1.0.0 | ||
*/ | ||
|
||
namespace Leaves_And_Love\OOP_Admin_Pages\Exception; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Exception thrown when an admin page is not found in a collection. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class Admin_Page_Not_Found_Exception extends Exception { | ||
|
||
} |
1 comment
on commit 84b6d68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Admin_Page_Collection
interface defines a container to hold the registered admin pages. This is not entirely necessary, but it's a common practice in WordPress to make registered objects available for third-party extensions. Having a collection to retrieve instances from makes this possible.
Another custom exception is also added, thrown in case an unregistered admin page should be retrieved.
It's usually a bad practice to provide different return types, since it means the consuming code needs to perform a check. Instead a lookup like this should either return a respective null object, or alternatively throw an exception, as in this example. A method to check whether an instance is registered should exist in order to work around throwing an exception.