From 6e0262de01a0b5f2b931a10d8af9affb0234b665 Mon Sep 17 00:00:00 2001 From: Stanislav Rejthar Date: Fri, 19 Jan 2018 23:30:58 +0100 Subject: [PATCH 1/2] ProjectCommon made dynamic MyCommon is ancestor for classes using MyCMS, i.e. MyController and (the new) MyAdmin --- classes/Application.php | 2 +- classes/MyAdmin.php | 24 ++++++++++++++++++ classes/MyCMS.php | 2 +- classes/MyCommon.php | 53 +++++++++++++++++++++++++++++++++++++++ classes/MyController.php | 34 +++++-------------------- classes/ProjectCommon.php | 28 +++++++++++++++------ classes/TableAdmin.php | 2 +- classes/TableLister.php | 2 +- 8 files changed, 108 insertions(+), 39 deletions(-) create mode 100644 classes/MyAdmin.php create mode 100644 classes/MyCommon.php diff --git a/classes/Application.php b/classes/Application.php index 3512aba9..22e98b1f 100644 --- a/classes/Application.php +++ b/classes/Application.php @@ -1,7 +1,7 @@ addAccepted(''); + parent::__construct($MyCMS, $options); + } + +} diff --git a/classes/MyCMS.php b/classes/MyCMS.php index 02d07751..eb0826c9 100644 --- a/classes/MyCMS.php +++ b/classes/MyCMS.php @@ -109,7 +109,7 @@ public function __construct(array $myCmsConf = array()) * * @param array $getArray $_GET or its equivalent * @param array $sessionArray $_SESSION or its equivalent - * @return bool $makeInclude for testing may be set to false as mycms itself does not contain the language-XX.inc.php files + * @param bool $makeInclude for testing may be set to false as mycms itself does not contain the language-XX.inc.php files * @return string to be used as $_SESSION['language'] * * constant TAB_PREFIX expected diff --git a/classes/MyCommon.php b/classes/MyCommon.php new file mode 100644 index 00000000..a774271e --- /dev/null +++ b/classes/MyCommon.php @@ -0,0 +1,53 @@ +addAccepted('get session'); + foreach (array_merge( + array(//default values + ), $options) as $optionVariable => $optionContent) { + if (in_array($optionVariable, $this->acceptedAttributes, true)) { + $this->{$optionVariable} = $optionContent; + } + } + $this->MyCMS = $MyCMS; + } + + /** + * + * @param string $acceptedList space delimited + */ + protected function addAccepted($acceptedList) + { + $this->acceptedAttributes = array_merge(explode(' ', $acceptedList), $this->acceptedAttributes ? $this->acceptedAttributes : array()); + } + +} diff --git a/classes/MyController.php b/classes/MyController.php index 29628183..3347568f 100644 --- a/classes/MyController.php +++ b/classes/MyController.php @@ -7,21 +7,15 @@ * * The default template is `home` */ -class MyController +class MyController extends MyCommon { - use \Nette\SmartObject; - - /** @var \GodsDev\MyCMS\MyCMS */ - protected $MyCMS; - /** @var array */ protected $result; - - /** @var array */ - protected $acceptedAttributes; - - //accepted attributes: + + /** + * accepted attributes: + */ /** @var array */ protected $get; @@ -37,26 +31,10 @@ class MyController public function __construct(MyCMS $MyCMS, array $options = array()) { $this->addAccepted('get session'); - foreach (array_merge( - array(//default values - ), $options) as $optionVariable => $optionContent) { - if (in_array($optionVariable, $this->acceptedAttributes, true)) { - $this->{$optionVariable} = $optionContent; - } - } - $this->MyCMS = $MyCMS; + parent::__construct($MyCMS, $options); $this->result = array("template" => "home", "context" => ($this->MyCMS->context ? $this->MyCMS->context : array())); } - /** - * - * @param string $acceptedList space delimited - */ - protected function addAccepted($acceptedList) - { - $this->acceptedAttributes = array_merge(explode(' ', $acceptedList), $this->acceptedAttributes ? $this->acceptedAttributes : array()); - } - /** * Outputs changed $MyCMS->template and $MyCMS->context as fields of an array * diff --git a/classes/ProjectCommon.php b/classes/ProjectCommon.php index a7916d1c..67207bff 100644 --- a/classes/ProjectCommon.php +++ b/classes/ProjectCommon.php @@ -2,17 +2,29 @@ namespace GodsDev\MyCMS; -use Psr\Log\LoggerInterface; - class ProjectCommon -{ +{ + use \Nette\SmartObject; - /** + /** @var \GodsDev\MyCMS\MyCMS */ + protected $MyCMS; + + /** + * + * @param \GodsDev\MyCMS\MyCMS $MyCMS + */ + public function __construct(MyCMS $MyCMS) + { + $this->MyCMS = $MyCMS; + } + + /** * Shortcut for echo'
'; var_dump(); and exit;
      * @param mixed variable(s) or expression to display
      */
-    public static function dump($var) {
+    public static function dump($var)
+    {
         echo '
';
         foreach (func_get_args() as $arg) {
             var_dump($arg);
@@ -21,7 +33,8 @@ public static function dump($var) {
     }
 
     //@todo refactor as getTexy() which returns Texy object that is initialized in this dynamic class
-    public static function prepareTexy() {
+    public static function prepareTexy()
+    {
         global $Texy;
         if (!is_object($Texy)) {
             $Texy = new \Texy();
@@ -37,7 +50,8 @@ public static function prepareTexy() {
      * @param string $stringOfTime
      * @param string $language
      */
-    public static function localDate($stringOfTime, $language) {
+    public static function localDate($stringOfTime, $language)
+    {
         switch ($language) {
             case 'cs': return date('j.n.Y', strtotime($stringOfTime));
         }
diff --git a/classes/TableAdmin.php b/classes/TableAdmin.php
index 694f31ff..e3bb1f90 100644
--- a/classes/TableAdmin.php
+++ b/classes/TableAdmin.php
@@ -2,7 +2,7 @@
 
 namespace GodsDev\MyCMS;
 
-use \GodsDev\Tools\Tools;
+use GodsDev\Tools\Tools;
 
 /**
  * This class facilitates administration of a database table
diff --git a/classes/TableLister.php b/classes/TableLister.php
index 99b1a9df..f58f5059 100644
--- a/classes/TableLister.php
+++ b/classes/TableLister.php
@@ -2,7 +2,7 @@
 
 namespace GodsDev\MyCMS;
 
-use \GodsDev\Tools\Tools;
+use GodsDev\Tools\Tools;
 
 /**
  * Class that can list rows of a database table, with editable search/filter 

From d8dd111ce13b329034534cf9d202e9f3c1ecd42d Mon Sep 17 00:00:00 2001
From: Stanislav Rejthar 
Date: Fri, 19 Jan 2018 23:47:59 +0100
Subject: [PATCH 2/2] fix MyAdmin dependence on MyCommon

---
 classes/MyAdmin.php      | 4 ++--
 classes/MyCommon.php     | 2 +-
 classes/MyController.php | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/classes/MyAdmin.php b/classes/MyAdmin.php
index 66e57237..5b506071 100644
--- a/classes/MyAdmin.php
+++ b/classes/MyAdmin.php
@@ -6,11 +6,11 @@
  * Parent for deployed Admin instance
  * 
  */
-class MyAdmin
+class MyAdmin extends MyCommon
 {
 
     /**
-     * Child may pre-populate $acceptedAttributes by addAcceptedmethod, all of them MUST be declared as variables
+     * Child may pre-populate $acceptedAttributes by addAcceptedmethod, all of them MUST be declared as protected/public variables
      * 
      * @param \GodsDev\MyCMS\MyCMS $MyCMS
      * @param array $options that overides default values within constructor
diff --git a/classes/MyCommon.php b/classes/MyCommon.php
index a774271e..d3381582 100644
--- a/classes/MyCommon.php
+++ b/classes/MyCommon.php
@@ -23,7 +23,7 @@ class MyCommon
 
 
     /**
-     * Child may pre-populate $acceptedAttributes by addAcceptedmethod, all of them MUST be declared as variables
+     * Child may pre-populate $acceptedAttributes by addAcceptedmethod, all of them MUST be declared as protected/public variables
      * 
      * @param \GodsDev\MyCMS\MyCMS $MyCMS
      * @param array $options that overides default values within constructor
diff --git a/classes/MyController.php b/classes/MyController.php
index 3347568f..6ff9a2f1 100644
--- a/classes/MyController.php
+++ b/classes/MyController.php
@@ -23,7 +23,7 @@ class MyController extends MyCommon
     protected $session;
 
     /**
-     * Child may pre-populate $acceptedAttributes by addAcceptedmethod, all of them MUST be declared as variables
+     * Child may pre-populate $acceptedAttributes by addAcceptedmethod, all of them MUST be declared as protected/public variables
      * 
      * @param \GodsDev\MyCMS\MyCMS $MyCMS
      * @param array $options that overides default values within constructor