@@ -10,13 +10,13 @@ discard block |
||
| 10 | 10 | * @link https://github.com/askupasoftware/amarkal-ui |
| 11 | 11 | * @copyright 2017 Askupa Software |
| 12 | 12 | */ |
| 13 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
| 13 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * Prevent loading the library more than once |
| 17 | 17 | */ |
| 18 | -if( defined( 'AMARKAL_UI' ) ) return; |
|
| 19 | -define( 'AMARKAL_UI', true ); |
|
| 18 | +if (defined('AMARKAL_UI')) return; |
|
| 19 | +define('AMARKAL_UI', true); |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Load required classes |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | require_once 'renderer.php'; |
| 25 | 25 | require_once 'abstract-component.php'; |
| 26 | 26 | |
| 27 | -if(!function_exists('amarkal_ui_render')) |
|
| 27 | +if (!function_exists('amarkal_ui_render')) |
|
| 28 | 28 | { |
| 29 | 29 | /** |
| 30 | 30 | * Render a UI component. |
@@ -34,14 +34,14 @@ discard block |
||
| 34 | 34 | * @param array $props The component's properties |
| 35 | 35 | * @return string The rendered HTML |
| 36 | 36 | */ |
| 37 | - function amarkal_ui_render( $type, array $props = array() ) |
|
| 37 | + function amarkal_ui_render($type, array $props = array()) |
|
| 38 | 38 | { |
| 39 | 39 | $renderer = Amarkal\UI\Renderer::get_instance(); |
| 40 | - return $renderer->render_component( $type, $props ); |
|
| 40 | + return $renderer->render_component($type, $props); |
|
| 41 | 41 | } |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | -if(!function_exists('amarkal_ui_register_component')) |
|
| 44 | +if (!function_exists('amarkal_ui_register_component')) |
|
| 45 | 45 | { |
| 46 | 46 | /** |
| 47 | 47 | * Register a custom UI component. The registered component's class should |
@@ -51,9 +51,9 @@ discard block |
||
| 51 | 51 | * to one of the core component's type, it will override the core component. |
| 52 | 52 | * @param string $class_name The component's class name. |
| 53 | 53 | */ |
| 54 | - function amarkal_ui_register_component( $type, $class_name ) |
|
| 54 | + function amarkal_ui_register_component($type, $class_name) |
|
| 55 | 55 | { |
| 56 | 56 | $renderer = Amarkal\UI\Renderer::get_instance(); |
| 57 | - $renderer->register_component( $type, $class_name ); |
|
| 57 | + $renderer->register_component($type, $class_name); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | \ No newline at end of file |
@@ -15,7 +15,9 @@ |
||
| 15 | 15 | /** |
| 16 | 16 | * Prevent loading the library more than once |
| 17 | 17 | */ |
| 18 | -if( defined( 'AMARKAL_UI' ) ) return; |
|
| 18 | +if( defined( 'AMARKAL_UI' ) ) { |
|
| 19 | + return; |
|
| 20 | +} |
|
| 19 | 21 | define( 'AMARKAL_UI', true ); |
| 20 | 22 | |
| 21 | 23 | /** |
@@ -18,16 +18,16 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | public static function get_instance() |
| 20 | 20 | { |
| 21 | - if( null === static::$instance ) |
|
| 21 | + if (null === static::$instance) |
|
| 22 | 22 | { |
| 23 | 23 | static::$instance = new static(); |
| 24 | 24 | } |
| 25 | 25 | return static::$instance; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - public function render_component( $type, array $props = array() ) |
|
| 28 | + public function render_component($type, array $props = array()) |
|
| 29 | 29 | { |
| 30 | - $component = $this->create_component( $type, $props ); |
|
| 30 | + $component = $this->create_component($type, $props); |
|
| 31 | 31 | return $component->render(); |
| 32 | 32 | } |
| 33 | 33 | |
@@ -42,9 +42,9 @@ discard block |
||
| 42 | 42 | * @param string $class_name |
| 43 | 43 | * @throws \RuntimeException |
| 44 | 44 | */ |
| 45 | - public function register_component( $type, $class_name ) |
|
| 45 | + public function register_component($type, $class_name) |
|
| 46 | 46 | { |
| 47 | - if( !in_array($type, $this->registered_components) ) |
|
| 47 | + if (!in_array($type, $this->registered_components)) |
|
| 48 | 48 | { |
| 49 | 49 | $this->registered_components[$type] = $class_name; |
| 50 | 50 | } |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | * @param type $props |
| 58 | 58 | * @return type |
| 59 | 59 | */ |
| 60 | - private function create_component( $type, $props ) |
|
| 60 | + private function create_component($type, $props) |
|
| 61 | 61 | { |
| 62 | 62 | try { |
| 63 | - $component = $this->create_registered_component ($type, $props ); |
|
| 63 | + $component = $this->create_registered_component($type, $props); |
|
| 64 | 64 | } catch (\RuntimeException $ex) { |
| 65 | - $component = $this->create_core_component ($type, $props ); |
|
| 65 | + $component = $this->create_core_component($type, $props); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | return $component; |
@@ -74,15 +74,15 @@ discard block |
||
| 74 | 74 | * @param type $props |
| 75 | 75 | * @throws \RuntimeException |
| 76 | 76 | */ |
| 77 | - private function create_core_component( $type, $props ) |
|
| 77 | + private function create_core_component($type, $props) |
|
| 78 | 78 | { |
| 79 | 79 | $file_name = __DIR__."/components/$type/controller.php"; |
| 80 | 80 | $class_name = 'Amarkal\\UI\\Component_'.$type; |
| 81 | 81 | |
| 82 | 82 | // Load one of the core components |
| 83 | - if(!class_exists($class_name)) |
|
| 83 | + if (!class_exists($class_name)) |
|
| 84 | 84 | { |
| 85 | - if( file_exists( $file_name ) ) |
|
| 85 | + if (file_exists($file_name)) |
|
| 86 | 86 | { |
| 87 | 87 | require_once $file_name; |
| 88 | 88 | } |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | * @param type $props |
| 102 | 102 | * @throws \RuntimeException |
| 103 | 103 | */ |
| 104 | - private function create_registered_component( $type, $props ) |
|
| 104 | + private function create_registered_component($type, $props) |
|
| 105 | 105 | { |
| 106 | - if(in_array($type, $this->registered_components)) |
|
| 106 | + if (in_array($type, $this->registered_components)) |
|
| 107 | 107 | { |
| 108 | 108 | $class_name = $this->registered_components[$type]; |
| 109 | 109 | return new $class_name($props); |
@@ -47,8 +47,9 @@ discard block |
||
| 47 | 47 | if( !in_array($type, $this->registered_components) ) |
| 48 | 48 | { |
| 49 | 49 | $this->registered_components[$type] = $class_name; |
| 50 | + } else { |
|
| 51 | + throw new \RuntimeException("A component with a name of '$type' has already been registered."); |
|
| 50 | 52 | } |
| 51 | - else throw new \RuntimeException("A component with a name of '$type' has already been registered."); |
|
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | /** |
@@ -85,8 +86,7 @@ discard block |
||
| 85 | 86 | if( file_exists( $file_name ) ) |
| 86 | 87 | { |
| 87 | 88 | require_once $file_name; |
| 88 | - } |
|
| 89 | - else |
|
| 89 | + } else |
|
| 90 | 90 | { |
| 91 | 91 | throw new \RuntimeException("The component '$type' does not exist."); |
| 92 | 92 | } |
@@ -18,18 +18,18 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @param array $model |
| 20 | 20 | */ |
| 21 | - public function __construct( array $model = array() ) |
|
| 21 | + public function __construct(array $model = array()) |
|
| 22 | 22 | { |
| 23 | 23 | // Check that the required parameters are provided. |
| 24 | - foreach( $this->required_parameters() as $key ) |
|
| 24 | + foreach ($this->required_parameters() as $key) |
|
| 25 | 25 | { |
| 26 | - if ( !isset($model[$key]) ) |
|
| 26 | + if (!isset($model[$key])) |
|
| 27 | 27 | { |
| 28 | 28 | throw new \RuntimeException('The required parameter "'.$key.'" was not provided for '.get_called_class()); |
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - $this->model = array_merge( $this->default_model(), $model ); |
|
| 32 | + $this->model = array_merge($this->default_model(), $model); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -39,9 +39,9 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @return mixed the parameter's value. |
| 41 | 41 | */ |
| 42 | - public function __get( $name ) |
|
| 42 | + public function __get($name) |
|
| 43 | 43 | { |
| 44 | - if( isset( $this->model[$name] ) ) |
|
| 44 | + if (isset($this->model[$name])) |
|
| 45 | 45 | { |
| 46 | 46 | return $this->model[$name]; |
| 47 | 47 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @return mixed the settings' parameter value. |
| 57 | 57 | */ |
| 58 | - public function __set( $name, $value ) |
|
| 58 | + public function __set($name, $value) |
|
| 59 | 59 | { |
| 60 | 60 | $this->model[$name] = $value; |
| 61 | 61 | } |
@@ -67,22 +67,22 @@ discard block |
||
| 67 | 67 | * @throws TemplateNotFoundException Thrown if the template file can |
| 68 | 68 | * not found. |
| 69 | 69 | */ |
| 70 | - public function render( $echo = false ){ |
|
| 70 | + public function render($echo = false) { |
|
| 71 | 71 | |
| 72 | 72 | $rendered_html = ''; |
| 73 | 73 | |
| 74 | - if( file_exists( $this->get_script_path() ) ) |
|
| 74 | + if (file_exists($this->get_script_path())) |
|
| 75 | 75 | { |
| 76 | 76 | ob_start(); |
| 77 | - include( $this->get_script_path() ); |
|
| 77 | + include($this->get_script_path()); |
|
| 78 | 78 | $rendered_html = ob_get_clean(); |
| 79 | 79 | } |
| 80 | 80 | else |
| 81 | 81 | { |
| 82 | - throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_script_path() ); |
|
| 82 | + throw new \RuntimeException("Error: cannot render HTML, template file not found at ".$this->get_script_path()); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - if( !$echo ) |
|
| 85 | + if (!$echo) |
|
| 86 | 86 | { |
| 87 | 87 | return $rendered_html; |
| 88 | 88 | } |
@@ -76,8 +76,7 @@ |
||
| 76 | 76 | ob_start(); |
| 77 | 77 | include( $this->get_script_path() ); |
| 78 | 78 | $rendered_html = ob_get_clean(); |
| 79 | - } |
|
| 80 | - else |
|
| 79 | + } else |
|
| 81 | 80 | { |
| 82 | 81 | throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_script_path() ); |
| 83 | 82 | } |