FEATURE: move bussiness logic out of models

This commit is contained in:
Michal Kváček
2016-10-17 01:05:20 +02:00
parent 2ded2924a1
commit e26e6fd460
46 changed files with 1069 additions and 1039 deletions
@@ -0,0 +1,78 @@
<?php
namespace App\Models\Templates\ModelTraits;
use App\Models\Templates\TemplatePath;
use App\Models\Templates\Template as TemplateModel;
trait Template {
protected $pathModel = null;
/**
* In one path may be many templates
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getPath()
{
if (!$this->pathModel) {
$this->pathModel = TemplatePath::where('id', $this->path)
->first();
}
return $this->pathModel;
}
/**
* Getter for absolute path to template
*
* @param null $mode
*
* @return string
*/
public function getPathToTemplate($mode = null)
{
$templateName = $this->template_name;
$path = $this->getPath();
// delete namespace if given
if (strpos($this->template_name, '::')) {
list(, $templateName) = explode('::', $templateName);
}
// get template name, do not care about directories (they are already contained in path)
$parts = explode('.', $templateName);
$templateName = last($parts);
// just a shortcut for this unbelievable long constant name...
$_ = DIRECTORY_SEPARATOR;
// path parts
$absolutePath = base_path($path->path);
$templatePath = str_replace('.', $_, $templateName);
$file = $templatePath . TemplateModel::TEMPLATE_SUFFIX;
// compose two versions of path - original and with "custom" directory
$original = $absolutePath . $_ . $file;
$edited = $absolutePath . $_ . TemplateModel::EDITED_TEMPLATES_DIRECTORY . $_ . $file;
// decide which path will be returned
switch ($mode) {
case TemplateModel::CUSTOM_EDIT_DIRECTORY:
return $edited;
break;
case TemplateModel::ORIGINAL_DIRECTORY:
return $original;
break;
default:
if (file_exists($absolutePath . $_ . TemplateModel::EDITED_TEMPLATES_DIRECTORY . $_ . $file)) {
return $edited;
}
return $original;
break;
}
}
}
+2 -76
View File
@@ -1,14 +1,12 @@
<?php
namespace App\Models;
namespace App\Models\Templates;
use Illuminate\Database\Eloquent\Model;
/**
* Class CustomFieldType
*/
class Template extends Model
{
use ModelTraits\Template;
public $timestamps = true;
protected $fillable = [
'template_name',
@@ -18,82 +16,10 @@ class Template extends Model
'description',
'editable'
];
protected $guarded = [];
protected $pathModel = null;
const EDITED_TEMPLATES_DIRECTORY = 'custom';
const ASSETS_DIRECTORY = 'assets';
const MODULES_DIRECTORY = 'modules';
const TEMPLATE_SUFFIX = '.blade.php';
const CUSTOM_EDIT_DIRECTORY = 1;
const ORIGINAL_DIRECTORY = 2;
/**
* In one path may be many templates
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getPath()
{
if (!$this->pathModel) {
$this->pathModel = TemplatePath::where('id', $this->path)
->first();
}
return $this->pathModel;
}
/**
* Getter for absolute path to template
*
* @param null $mode
*
* @return string
*/
public function getPathToTemplate($mode = null)
{
$templateName = $this->template_name;
$path = $this->getPath();
// delete namespace if given
if (strpos($this->template_name, '::')) {
list(, $templateName) = explode('::', $templateName);
}
// get template name, do not care about directories (they are already contained in path)
$parts = explode('.', $templateName);
$templateName = last($parts);
// just a shortcut for this unbelievable long constant name...
$_ = DIRECTORY_SEPARATOR;
// path parts
$absolutePath = base_path($path->path);
$templatePath = str_replace('.', $_, $templateName);
$file = $templatePath . self::TEMPLATE_SUFFIX;
// compose two versions of path - original and with "custom" directory
$original = $absolutePath . $_ . $file;
$edited = $absolutePath . $_ . self::EDITED_TEMPLATES_DIRECTORY . $_ . $file;
// decide which path will be returned
switch ($mode) {
case self::CUSTOM_EDIT_DIRECTORY:
return $edited;
break;
case self::ORIGINAL_DIRECTORY:
return $original;
break;
default:
if (file_exists($absolutePath . $_ . self::EDITED_TEMPLATES_DIRECTORY . $_ . $file)) {
return $edited;
}
return $original;
break;
}
}
}
+9 -5
View File
@@ -1,6 +1,6 @@
<?php
namespace App\Models;
namespace App\Models\Templates;
use Illuminate\Database\Eloquent\Model;
@@ -10,8 +10,12 @@ use Illuminate\Database\Eloquent\Model;
class TemplatePath extends Model
{
public $timestamps = true;
protected $fillable = ['name', 'path', 'parent_id', 'namespace'];
protected $guarded = [];
protected $fillable = [
'name',
'path',
'parent_id',
'namespace'
];
/**
* In one path may be many templates
@@ -20,7 +24,7 @@ class TemplatePath extends Model
*/
public function templates()
{
return $this->hasMany('App\Models\Template', 'path');
return $this->hasMany(Template::class, 'path');
}
/**
@@ -30,6 +34,6 @@ class TemplatePath extends Model
*/
public function subdirectories()
{
return $this->hasMany('App\Models\TemplatePath', 'parent_id');
return $this->hasMany(TemplatePath::class, 'parent_id');
}
}