-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbc7c82
commit 750795a
Showing
6 changed files
with
217 additions
and
156 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
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,53 @@ | ||
<?php | ||
|
||
namespace Imanghafoori\Widgets\Utils\Normalizers; | ||
|
||
class CacheNormalizer | ||
{ | ||
/** | ||
* ّFigures out how long the cache life time should be. | ||
* @param $widget | ||
* @return null | ||
*/ | ||
public function normalizeCacheLifeTime($widget) | ||
{ | ||
if ($widget->cacheLifeTime === 'env_default') { | ||
$widget->cacheLifeTime = (int)(env('WIDGET_DEFAULT_CACHE_LIFETIME', 0)); | ||
} | ||
|
||
if ($widget->cacheLifeTime === 'forever') { | ||
$widget->cacheLifeTime = -1; | ||
} | ||
} | ||
|
||
/** | ||
* ّFigures out what the cache tags should be. | ||
* @param $widget | ||
* @return null | ||
*/ | ||
public function normalizeCacheTags($widget) | ||
{ | ||
if (!$this->cacheCanUseTags() || !$widget->cacheTags) { | ||
return $widget->cacheTags = null; | ||
} | ||
|
||
if (is_array($widget->cacheTags)) { | ||
return $widget->cacheTags; | ||
} | ||
|
||
if (is_string($widget->cacheTags)) { | ||
return $widget->cacheTags = [$widget->cacheTags]; | ||
} | ||
|
||
throw new \InvalidArgumentException('Cache Tags should be of type String or Array.'); | ||
} | ||
|
||
/** | ||
* Determine whether cache tags should be applied or not | ||
* @return bool | ||
*/ | ||
private function cacheCanUseTags() | ||
{ | ||
return !in_array(env('CACHE_DRIVER', 'file'), ['file', 'database']); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace Imanghafoori\Widgets\Utils\Normalizers; | ||
|
||
class ControllerNormalizer | ||
{ | ||
/** | ||
* Figures out which method should be called as the controller. | ||
* @param $widget | ||
* @return null | ||
*/ | ||
public function normalizeControllerMethod($widget) | ||
{ | ||
// We decide to call data method on widget object by default. | ||
$controllerMethod = [$widget, 'data']; | ||
$ctrlClass = get_class($widget); | ||
|
||
// If the user has explicitly declared controller class path on widget | ||
// then we decide to call data method on that instead. | ||
if ($widget->controller) { | ||
$ctrlClass = $widget->controller; | ||
$controllerMethod = ($ctrlClass) . '@data'; | ||
} | ||
|
||
$this->checkControllerExists($ctrlClass); | ||
$this->checkDataMethodExists($ctrlClass); | ||
|
||
$widget->controller = $controllerMethod; | ||
} | ||
|
||
/** | ||
* @param $ctrlClass | ||
*/ | ||
private function checkControllerExists($ctrlClass) | ||
{ | ||
if (!class_exists($ctrlClass)) { | ||
throw new \InvalidArgumentException("Controller class: [{$ctrlClass}] not found."); | ||
} | ||
} | ||
|
||
/** | ||
* @param $ctrlClass | ||
*/ | ||
private function checkDataMethodExists($ctrlClass) | ||
{ | ||
if (!method_exists($ctrlClass, 'data')) { | ||
throw new \InvalidArgumentException("'data' method not found on " . $ctrlClass); | ||
} | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace Imanghafoori\Widgets\Utils\Normalizers; | ||
|
||
class PresenterNormalizer | ||
{ | ||
/** | ||
* Figures out which method should be called as the presenter | ||
* @return null | ||
*/ | ||
public function normalizePresenterName($widget) | ||
{ | ||
if ($widget->presenter !== 'default') { | ||
$presenter = $widget->presenter; | ||
$this->checkPresenterExists($presenter); | ||
} else { | ||
$presenter = get_class($widget) . 'Presenter'; | ||
if (!class_exists($presenter)) { | ||
return $widget->presenter = null; | ||
} | ||
} | ||
|
||
$this->checkPresentMethodExists($presenter); | ||
|
||
$widget->presenter = $presenter . '@present'; | ||
} | ||
|
||
/** | ||
* @param $presenter | ||
*/ | ||
private function checkPresentMethodExists($presenter) | ||
{ | ||
if (!method_exists($presenter, 'present')) { | ||
throw new \InvalidArgumentException("'present' method not found on : " . $presenter); | ||
} | ||
} | ||
|
||
/** | ||
* @param $presenter | ||
*/ | ||
private function checkPresenterExists($presenter) | ||
{ | ||
if (!class_exists($presenter)) { | ||
throw new \InvalidArgumentException("Presenter Class [{$presenter}] not found."); | ||
} | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Imanghafoori\Widgets\Utils\Normalizers; | ||
|
||
class TemplateNormalizer | ||
{ | ||
/** | ||
* Figures out which template to render. | ||
* @param $widget | ||
* @return null | ||
*/ | ||
public function normalizeTemplateName($widget) | ||
{ | ||
// class name without namespace. | ||
$className = str_replace('App\\Widgets\\', '', class_basename($widget)); | ||
|
||
// replace slashes with dots | ||
$className = str_replace(['\\', '/'], '.', $className); | ||
|
||
if ($widget->template === null) { | ||
$widget->template = 'Widgets::' . $className . 'View'; | ||
} | ||
|
||
if (!view()->exists($widget->template)) { | ||
throw new \InvalidArgumentException("View file [{$className}View] not found by: '" . class_basename($widget) . " '"); | ||
} | ||
} | ||
} |
Oops, something went wrong.