From be6b3502be32708b57b23a560d9f618b589b57ea Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 8 Nov 2017 13:04:49 +0100 Subject: [PATCH] Created 'i_patch' to patch the 'i_view' each one have the same name as the associated 'warehouse module' + it can bind multiple files to a main's block each + done machine/view + its patch 'motheure' + machine refactored (blockized the templates) --- build/generic/core/View.php | 64 ++++++++-- build/generic/core/i_patch.php | 13 ++ build/generic/core/i_view.php | 63 ++++++++-- .../view/machine/form/create/main.twig | 16 ++- .../generic/view/machine/form/edit/main.twig | 43 +++++-- .../view/machine/form/remove/main.twig | 41 +++++-- build/generic/view/machine/group/main.twig | 116 +++++++++++------- build/generic/view/machine/view/main.php | 20 --- build/generic/view/machine/view/main.twig | 72 ++++++----- build/generic/view/machine/view/motheure.php | 36 ++++++ .../view/machine/view/motheure_count.twig | 10 ++ 11 files changed, 355 insertions(+), 139 deletions(-) create mode 100644 build/generic/core/i_patch.php create mode 100644 build/generic/view/machine/view/motheure.php create mode 100644 build/generic/view/machine/view/motheure_count.twig diff --git a/build/generic/core/View.php b/build/generic/core/View.php index bb80c1a..7888907 100644 --- a/build/generic/core/View.php +++ b/build/generic/core/View.php @@ -7,19 +7,55 @@ /* (1) Attributes ---------------------------------------------------------*/ - private $view_class; + private $core_class; + private $patch_class = []; private $arguments; public static $html_error = "Une erreur est survenue, veuilez contacter le webmaster si cette erreur persiste."; - /* (2) Instance constructor + /* (2) Instance constructor (add patches) * - * @view_class The target view class + * @core_class The target view class * ---------------------------------------------------------*/ - private function __construct(String $view_class){ - $this->view_class = $view_class; + private function __construct(String $core_class){ + + /* (1) Get class directory & namespace */ + $root_ns = str_replace('/', '\\', dirname( str_replace('\\', '/', $core_class) ) ).'\\'; + $root_dir = __BUILD__.'/generic/'.dirname( str_replace('\\', '/', $core_class) ); + + /* (2) Get patches */ + foreach( glob($root_dir.'/*.php') as $class ){ + + // {1} Extract basename (without '.php') // + $basename = basename($class); + $basename = substr($basename, 0, strlen($basename)-strlen('.php')); + $class_ns = $root_ns.$basename; + + // {2} Ignore main (core class) // + if( $basename == 'main' ) + continue; + + // {3} Check if class exists // + if( !class_exists($class_ns) ) + continue; + + // {4} Check if instance of 'i_patch' // + if( !(new $class_ns() instanceof i_patch) ) + continue; + + // {5} Check if it corresponds to a warehouse's module // + if( !in_array($basename, $_SESSION['WAREHOUSE']['modules']) ) + continue; + + // {6} Store each patch instance // + $this->patch_class[$basename] = new $class_ns(); + + } + + /* (3) Store core class */ + $this->core_class = $core_class; } @@ -31,12 +67,15 @@ * @return render Rendered view * ---------------------------------------------------------*/ - public function render($injected_data=[]){ + public function render(array $injected_data=[]){ + /* (1) Create core instance with data */ + $view = new $this->core_class(...$injected_data); - /* (1) Create instance with data */ - $view = new $this->view_class(...$injected_data); + /* (2) Patch it every @patch_class */ + foreach($this->patch_class as $patch_name=>$patch_inst) + $view->patch($patch_name, $patch_inst); - /* (2) Dispatch rendering */ + /* (3) Dispatch rendering */ return $view->render(); } @@ -59,21 +98,20 @@ return null; /* (2) Extract class */ - $view_class = '\\view\\'.str_replace('.', '\\', $view_path).'\\main'; + $core_class = '\\view\\'.str_replace('.', '\\', $view_path).'\\main'; /* (3) Check if class exists */ - if( !class_exists($view_class) ) + if( !class_exists($core_class) ) return null; /* (2) Return View instance ---------------------------------------------------------*/ - return new self($view_class); + return new self($core_class); } - } \ No newline at end of file diff --git a/build/generic/core/i_patch.php b/build/generic/core/i_patch.php new file mode 100644 index 0000000..d7d4d14 --- /dev/null +++ b/build/generic/core/i_patch.php @@ -0,0 +1,13 @@ + Patch name + * @patch_inst Patch instance + * + ---------------------------------------------------------*/ + public function patch(String $patch_name, i_patch $patch_inst){ + $this->patch[$patch_name] = $patch_inst; + } + + /* (3) Returns the auto patch code + * + * + ---------------------------------------------------------*/ + private function autopatch(){ + /* (1) Pre-code */ + $code = "{% extends 'main.twig' %}\n\n"; + + /* (2) For each module patch */ + foreach($this->patch as $name=>$inst) + + /* (3) For each block patch */ + foreach($inst->patch as $block=>$temp) + $code .= "{% block $block %}{% include '$temp' %}{% endblock %}\n"; + + + /* (4) Return code */ + return $code; + } + + + + /* (4) Renders the view * ---------------------------------------------------------*/ public function render(){ - /* (1) Get path information (child class) + /* (1) Get path information (core class) ---------------------------------------------------------*/ - /* (1) Get child class */ - $child_class = str_replace('\\', '/', get_class(debug_backtrace()[0]['object'])); + /* (1) Get core class */ + $core_class = str_replace('\\', '/', get_class($this)); /* (2) Extract root DIR */ - $root_path = __BUILD__.'/generic/'.dirname($child_class); + $root_path = __BUILD__.'/generic/'.dirname($core_class); /* (3) Extract file name */ - $base_path = basename($child_class).'.twig'; + $model_path = basename($core_class).'.twig'; + + + /* (2) Get patches + ---------------------------------------------------------*/ + $auto_patch = [ 'render.twig' => $this->autopatch() ]; /* (2) Setup ---------------------------------------------------------*/ /* (1) Initialize twig */ - $loader = new \Twig_Loader_Filesystem($root_path); + $loader = new \Twig_Loader_Chain([ + new \Twig_Loader_Filesystem($root_path), // default directory templates + new \Twig_Loader_Array($auto_patch) // custom auto-patch system + ]); + $twig = new \Twig_Environment($loader, [ 'debug' => true, 'cache' => false, @@ -36,7 +83,7 @@ /* (3) Build the view and return it back ---------------------------------------------------------*/ /* (1) Render and return the view */ - return $twig->render($base_path, [ 'core' => $this ]); + return $twig->render('render.twig', [ 'core' => $this, 'patch' => $this->patch ]); } diff --git a/build/generic/view/machine/form/create/main.twig b/build/generic/view/machine/form/create/main.twig index a7e8579..94651fe 100644 --- a/build/generic/view/machine/form/create/main.twig +++ b/build/generic/view/machine/form/create/main.twig @@ -1,9 +1,17 @@ -
+{% block form_tag %} {% endblock %} -
-
+ {% block input %} + +
+
+ + {% endblock %} - + {% block submit %} + + + + {% endblock %}
\ No newline at end of file diff --git a/build/generic/view/machine/form/edit/main.twig b/build/generic/view/machine/form/edit/main.twig index b0eaabc..b500b22 100644 --- a/build/generic/view/machine/form/edit/main.twig +++ b/build/generic/view/machine/form/edit/main.twig @@ -1,18 +1,43 @@ -
+{% block form_tag %} {% endblock %} -
- + {% block search %} - - machine 0 sur 0

-
+ {% block search_input %} + +
+ + + {% endblock %} + + + {% block search_submit %} + + machine 0 sur 0

+
+ + {% endblock %} + + {% endblock %}




-
-
- + {% block edit %} + + {% block edit_input %} + +
+
+ + {% endblock %} + + {% block edit_submit %} + + + + {% endblock %} + + {% endblock %}
\ No newline at end of file diff --git a/build/generic/view/machine/form/remove/main.twig b/build/generic/view/machine/form/remove/main.twig index f73404f..c3bc987 100644 --- a/build/generic/view/machine/form/remove/main.twig +++ b/build/generic/view/machine/form/remove/main.twig @@ -1,16 +1,41 @@ -
+{% block form_tag %} {% endblock %} -
- + {% block search %} - - machine 0 sur 0

-
+ {% block search_input %} + +
+ + + {% endblock %} + + + {% block search_submit %} + + machine 0 sur 0

+
+ + {% endblock %} + + {% endblock %}




-
- + {% block edit %} + + {% block edit_input %} +
+ + {% endblock %} + + {% block edit_submit %} + + + + {% endblock %} + + {% endblock %} +
\ No newline at end of file diff --git a/build/generic/view/machine/group/main.twig b/build/generic/view/machine/group/main.twig index cbc0e62..1ce7523 100644 --- a/build/generic/view/machine/group/main.twig +++ b/build/generic/view/machine/group/main.twig @@ -1,54 +1,82 @@ - - -{% for cluster in core.get_clusters() %} -
- - {% set machinelist = core.get_members(cluster.id_machine_cluster) %} - - {{ cluster.name }} - {{ core.icon.remove | raw }} - - {{ core.icon.edit | raw }} - - - {{ core.icon.device | raw }} - {{ machinelist | length }} machines - +{% block search_bar %} {% endblock %} - - {{ core.icon.option | raw }} - {% for option in core.get_options(cluster.id_machine_cluster) %} - - {{ option.name }}:{{ option.daemon }} - - {% else %} - Aucune option - {% endfor %} - +{% block group_list %} - - {{ core.icon.group | raw }} + {% for cluster in core.get_clusters() %} - - {% for machine in machinelist %} - - {{ machine.name }} - + {% block group_card %} + + {% block card_tag %}
{% endblock %} + + {% set machinelist = core.get_members(cluster.id_machine_cluster) %} + + {% block card_title %} {{ cluster.name }} {% endblock %} + + {% block card_remove %} {{ core.icon.remove | raw }} {% endblock %} + {% block card_edit %} {{ core.icon.edit | raw }} {% endblock %} + + {# To be patched #} + {% block card_patch %}{% endblock %} + + {% block card_count %} + + + {{ core.icon.device | raw }} + {{ machinelist | length }} machines - {% endfor %} - - + - + {% endblock %} -
+ {% block card_option %} -{# if no result #} -{% else %} + + {{ core.icon.option | raw }} + {% for option in core.get_options(cluster.id_machine_cluster) %} + + {{ option.name }}:{{ option.daemon }} + + {% else %} + Aucune option + {% endfor %} + -
- Aucun groupe trouvé. -
+ {% endblock %} -{% endfor %} + {% block card_group %} + + + {{ core.icon.group | raw }} + + + {% for machine in machinelist %} + + {{ machine.name }} + + + {% endfor %} + + + + + + + {% endblock %} + +
+ + {% endblock %} + + {# if no result #} + {% else %} + + {% block no_result %} + +
+ Aucun groupe trouvé. +
+ + {% endblock %} + + {% endfor %} + +{% endblock %} \ No newline at end of file diff --git a/build/generic/view/machine/view/main.php b/build/generic/view/machine/view/main.php index 7c5a2b4..9b1060e 100644 --- a/build/generic/view/machine/view/main.php +++ b/build/generic/view/machine/view/main.php @@ -79,24 +79,4 @@ return $answer->get('clusters'); } - - - - public function get_motheure($id_machine){ - /* (1) Get its machine_clusters - ---------------------------------------------------------*/ - /* (1) Create request */ - $motheureReq = new Request('motheure/getCount', ['id_machine' => $id_machine]); - - /* (2) Execute */ - $motheureRes = $motheureReq->dispatch(); - - /* (3) Manage error */ - if( $motheureRes->error->get() != Err::Success ) - return null; - - return $motheureRes->get('count'); - } - - } \ No newline at end of file diff --git a/build/generic/view/machine/view/main.twig b/build/generic/view/machine/view/main.twig index d4e2a7f..ee018ce 100644 --- a/build/generic/view/machine/view/main.twig +++ b/build/generic/view/machine/view/main.twig @@ -1,45 +1,51 @@ - +{% block search_bar %} {% endblock %} -{% for machine in core.get_machines() %} -
+{% block machine_list %} - - {{ machine.name }} #{{ machine.name }} - {{ core.icon.remove | raw }} + {% for machine in core.get_machines() %} - {{ core.icon.edit | raw }} + {% block machine_card %} - {# List etrees #} - {% set motheure = core.get_motheure(machine.id_machine) %} + {% block card_tag %}
{% endblock %} - {% if motheure %} - - {{ core.icon.motor | raw }} - {{ motheure }}ms - - {% endif %} + {% block card_state %} {% endblock %} + {% block card_title %} {{ machine.name }} #{{ machine.name }} {% endblock %} - - {{ core.icon.group | raw }} + {% block card_remove %} {{ core.icon.remove | raw }} {% endblock %} + {% block card_edit %} {{ core.icon.edit | raw }} {% endblock %} - - {% for cluster in core.get_clusters(machine.id_machine) %} - - {{ cluster.name }} - + {# To be patched #} + {% block card_motheure_patch %}{% endblock %} + + {% block card_group %} + + + {{ core.icon.group | raw }} + + + {% for cluster in core.get_clusters(machine.id_machine) %} + + {{ cluster.name }} + + + {% endfor %} + + + + - {% endfor %} - - + - + {% endblock %} -
+
-{# if no result #} -{% else %} + {% endblock %} -
- Aucune machine trouvée -
-{% endfor %} + {# if no result #} + {% else %} + +
+ Aucune machine trouvée +
+ {% endfor %} + +{% endblock %} \ No newline at end of file diff --git a/build/generic/view/machine/view/motheure.php b/build/generic/view/machine/view/motheure.php new file mode 100644 index 0000000..b5cb437 --- /dev/null +++ b/build/generic/view/machine/view/motheure.php @@ -0,0 +1,36 @@ + 'motheure_count.twig' + ]; + + + public function get_motheure($id_machine){ + /* (1) Get its machine_clusters + ---------------------------------------------------------*/ + /* (1) Create request */ + $motheureReq = new Request('motheure/getCount', ['id_machine' => $id_machine]); + + /* (2) Execute */ + $motheureRes = $motheureReq->dispatch(); + + /* (3) Manage error */ + if( $motheureRes->error->get() != Err::Success ) + return null; + + return $motheureRes->get('count'); + } + + + } \ No newline at end of file diff --git a/build/generic/view/machine/view/motheure_count.twig b/build/generic/view/machine/view/motheure_count.twig new file mode 100644 index 0000000..07f36a4 --- /dev/null +++ b/build/generic/view/machine/view/motheure_count.twig @@ -0,0 +1,10 @@ +{% set motheure = patch.motheure.get_motheure(machine.id_machine) %} + +{% if motheure %} + + + {{ core.icon.motor | raw }} + {{ motheure }}ms + + +{% endif %} \ No newline at end of file