From e92c336420fb7848f87dc32c48298147d6e17fa0 Mon Sep 17 00:00:00 2001 From: tylercraig9332 Date: Thu, 14 Jun 2018 16:01:07 -0400 Subject: [PATCH 1/2] Moved some stuff from controller into view. --- class/Controller/RoleController.php | 8 ++- class/Controller/Show/Admin.php | 29 ++++------ class/Controller/Show/Base.php | 6 ++ class/Factory/Base.php | 70 ------------------------ class/View/BaseView.php | 85 +++++++++++++++++++++++++++++ class/View/ShowView.php | 34 ++++++++++++ 6 files changed, 143 insertions(+), 89 deletions(-) create mode 100644 class/View/BaseView.php create mode 100644 class/View/ShowView.php diff --git a/class/Controller/RoleController.php b/class/Controller/RoleController.php index c99d818..49f7e59 100644 --- a/class/Controller/RoleController.php +++ b/class/Controller/RoleController.php @@ -27,14 +27,18 @@ abstract class RoleController { protected $factory; + protected $view; protected $role; protected $id; abstract protected function loadFactory(); + abstract protected function loadView(); + public function __construct($role) { $this->loadFactory(); + $this->loadView(); $this->role = $role; } @@ -108,7 +112,7 @@ public function put(Request $request) } else { $method_name = $command . 'PutCommand'; } - + if (!method_exists($this, $method_name)) { throw new BadCommand($method_name); } @@ -131,6 +135,8 @@ public function getHtml(Request $request) if ($this->id && method_exists($this, 'viewHtmlCommand')) { $method_name = 'viewHtmlCommand'; } else { + //echo(method_exists($this, 'viewHtmlCommand')); + var_dump($this->id); throw new BadCommand($method_name); } } diff --git a/class/Controller/Show/Admin.php b/class/Controller/Show/Admin.php index cf6c4d8..64f7319 100644 --- a/class/Controller/Show/Admin.php +++ b/class/Controller/Show/Admin.php @@ -27,8 +27,8 @@ namespace slideshow\Controller\Show; use Canopy\Request; -use slideshow\Factory\NavBar; use slideshow\Factory\ShowFactory; +use slideshow\View\ShowView; class Admin extends Base { @@ -38,18 +38,20 @@ class Admin extends Base */ protected $factory; - public function postCommand(Request $request) + /** + * @var slideshow\View\ShowView + */ + protected $view; + + protected function listHtmlCommand(Request $request) { - $show = $this->factory->post($request); - return array('show'=>$show->getStringVars()); + return $this->view->show(); } - protected function listHtmlCommand(Request $request) + public function postCommand(Request $request) { - $script = $this->factory->scriptView('shows'); - \Layout::addJSHeader($script); - $this->createShowButton(); - return $this->factory->scriptView('shows'); + $show = $this->factory->post($request); + return array('show'=>$show->getStringVars()); } protected function listJsonCommand(Request $request) @@ -86,13 +88,4 @@ protected function getJsonView($data, \Canopy\Request $request) return new \phpws2\View\JsonView($result); } - private function createShowButton() - { - $nav = new NavBar(); - $create = << Create New SlideShow -EOF; - $nav->addItem($create); - } - } diff --git a/class/Controller/Show/Base.php b/class/Controller/Show/Base.php index bed2b39..52b2ecd 100644 --- a/class/Controller/Show/Base.php +++ b/class/Controller/Show/Base.php @@ -20,6 +20,7 @@ use Canopy\Request; use slideshow\Factory\ShowFactory as Factory; +use slideshow\View\ShowView as View; use slideshow\Controller\RoleController; class Base extends RoleController @@ -35,6 +36,11 @@ protected function loadFactory() $this->factory = new Factory; } + protected function loadView() + { + $this->view = new View; + } + protected function viewHtmlCommand(Request $request) { return 'Show.Base::viewHtmlCommand'; diff --git a/class/Factory/Base.php b/class/Factory/Base.php index a2ba8a2..ddd74c2 100644 --- a/class/Factory/Base.php +++ b/class/Factory/Base.php @@ -42,74 +42,4 @@ function($letter) { } } - private function getScript($filename) - { - $root_directory = PHPWS_SOURCE_HTTP . 'mod/slideshow/javascript/'; - if (SLIDESHOW_REACT_DEV) { - $path = "dev/$filename.js"; - } else { - $path = "build/$filename.js"; - } - $script = ""; - return $script; - } - - public function scriptView($view_name, $add_anchor = true, $vars = null) - { - static $vendor_included = false; - if (!$vendor_included) { - $script[] = $this->getScript('vendor'); - $vendor_included = true; - } - if (!empty($vars)) { - $script[] = $this->addScriptVars($vars); - } - $script[] = $this->getScript($view_name); - $react = implode("\n", $script); - if ($add_anchor) { - $content = << -$react -EOF; - return $content; - } else { - return $react; - } - } - - private function addScriptVars($vars) - { - if (empty($vars)) { - return null; - } - foreach ($vars as $key => $value) { - $varList[] = "const $key = '$value';"; - } - return ''; - } - - protected function getRootDirectory() - { - return PHPWS_SOURCE_DIR . 'mod/slideshow/'; - } - - protected function getRootUrl() - { - return PHPWS_SOURCE_HTTP . 'mod/slideshow/'; - } - - private function getAssetPath($scriptName) - { - $rootDirectory = $this->getRootDirectory(); - if (!is_file($rootDirectory . 'assets.json')) { - exit('Missing assets.json file. Run npm run prod in stories directory.'); - } - $jsonRaw = file_get_contents($rootDirectory . 'assets.json'); - $json = json_decode($jsonRaw, true); - if (!isset($json[$scriptName]['js'])) { - throw new \Exception('Script file not found among assets.'); - } - return $json[$scriptName]['js']; - } - } diff --git a/class/View/BaseView.php b/class/View/BaseView.php new file mode 100644 index 0000000..8bce690 --- /dev/null +++ b/class/View/BaseView.php @@ -0,0 +1,85 @@ + + * @license https://opensource.org/licenses/MIT + */ + namespace slideshow\View; + + abstract class BaseView + { + + private function getScript($filename) + { + $root_directory = PHPWS_SOURCE_HTTP . 'mod/slideshow/javascript/'; + if (SLIDESHOW_REACT_DEV) { + $path = "dev/$filename.js"; + } else { + $path = "build/$filename.js"; + } + $script = ""; + return $script; + } + + public function scriptView($view_name, $add_anchor = true, $vars = null) + { + static $vendor_included = false; + if (!$vendor_included) { + $script[] = $this->getScript('vendor'); + $vendor_included = true; + } + if (!empty($vars)) { + $script[] = $this->addScriptVars($vars); + } + $script[] = $this->getScript($view_name); + $react = implode("\n", $script); + if ($add_anchor) { + $content = << +$react +EOF; + return $content; + } else { + return $react; + } + } + + private function addScriptVars($vars) + { + if (empty($vars)) { + return null; + } + foreach ($vars as $key => $value) { + $varList[] = "const $key = '$value';"; + } + return ''; + } + + protected function getRootDirectory() + { + return PHPWS_SOURCE_DIR . 'mod/slideshow/'; + } + + protected function getRootUrl() + { + return PHPWS_SOURCE_HTTP . 'mod/slideshow/'; + } + + private function getAssetPath($scriptName) + { + $rootDirectory = $this->getRootDirectory(); + if (!is_file($rootDirectory . 'assets.json')) { + exit('Missing assets.json file. Run npm run prod in stories directory.'); + } + $jsonRaw = file_get_contents($rootDirectory . 'assets.json'); + $json = json_decode($jsonRaw, true); + if (!isset($json[$scriptName]['js'])) { + throw new \Exception('Script file not found among assets.'); + } + return $json[$scriptName]['js']; + } + } diff --git a/class/View/ShowView.php b/class/View/ShowView.php new file mode 100644 index 0000000..7faf49f --- /dev/null +++ b/class/View/ShowView.php @@ -0,0 +1,34 @@ + + * @author Tyler Craig + * @license https://opensource.org/licenses/MIT + */ + + namespace slideshow\View; + + use slideshow\Factory\NavBar; + + class ShowView extends BaseView + { + + public function show() + { + $this->createShowButton(); + return $this->scriptView('shows'); + } + + private function createShowButton() + { + $nav = new NavBar(); + $create = << Create New SlideShow +EOF; + $nav->addItem($create); + } + } From 6edb67284f2b170334ab6ca6e4ffe68305f5e365 Mon Sep 17 00:00:00 2001 From: tylercraig9332 Date: Thu, 14 Jun 2018 16:05:25 -0400 Subject: [PATCH 2/2] Removed a debug call. --- class/Controller/RoleController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/class/Controller/RoleController.php b/class/Controller/RoleController.php index 49f7e59..a46342d 100644 --- a/class/Controller/RoleController.php +++ b/class/Controller/RoleController.php @@ -135,8 +135,6 @@ public function getHtml(Request $request) if ($this->id && method_exists($this, 'viewHtmlCommand')) { $method_name = 'viewHtmlCommand'; } else { - //echo(method_exists($this, 'viewHtmlCommand')); - var_dump($this->id); throw new BadCommand($method_name); } }