FEATURE: change directory structure
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace App\CMS\Handlers\Registrators\Items;
|
||||
|
||||
class SettingExtension
|
||||
{
|
||||
/**
|
||||
* @var string human readable ID of settings section (used e.g. as ID parameter in html)
|
||||
*/
|
||||
protected $id;
|
||||
/**
|
||||
* @var string translations string used for describing briefly settings section
|
||||
*/
|
||||
protected $name;
|
||||
/**
|
||||
* @var string view used for rendering given settings part
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
/**
|
||||
* SettingExtension constructor.
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $name
|
||||
* @param string $view
|
||||
*/
|
||||
public function __construct($id, $name, $view)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getView()
|
||||
{
|
||||
return $this->view;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace App\CMS\Handlers\Registrators;
|
||||
|
||||
use App\CMS\Widgets\Contracts\MenuExtensionHandler;
|
||||
|
||||
class MenuExtensionRegistrator {
|
||||
|
||||
protected $menuExtensions;
|
||||
|
||||
/**
|
||||
* @param MenuExtensionHandler $handler
|
||||
*/
|
||||
public function register(MenuExtensionHandler $handler) {
|
||||
$this->menuExtensions[] = $handler;
|
||||
}
|
||||
|
||||
public function get(){
|
||||
return $this->menuExtensions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace App\CMS\Handlers\Registrators;
|
||||
|
||||
use App\CMS\Enumerations\Exceptions\EnumerationExistsException;
|
||||
use App\CMS\Handlers\Contracts\SearchHandler;
|
||||
use App\CMS\Handlers\Contracts\SearchPresenterHandler;
|
||||
use App\CMS\Handlers\Registrators\Items\SearchEngine;
|
||||
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
||||
|
||||
class SearchEngineRegistrator
|
||||
{
|
||||
const ADMIN = 'admin';
|
||||
const VISITOR = 'visitor';
|
||||
protected $searchEngines;
|
||||
|
||||
function __construct($app)
|
||||
{
|
||||
$this->searchEngines = [
|
||||
self::ADMIN => [],
|
||||
self::VISITOR => []
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for registering new settings extensions/parts
|
||||
*
|
||||
* @param SearchHandler $search
|
||||
* @param SearchPresenterHandler $presenter
|
||||
*
|
||||
* @param $type
|
||||
*
|
||||
* @throws EnumerationExistsException
|
||||
*/
|
||||
public function register(SearchHandler $search, SearchPresenterHandler $presenter, $type)
|
||||
{
|
||||
$this->checkIfRegistered($type, $search->getId());
|
||||
|
||||
// register new extension
|
||||
$this->searchEngines[$type][$search->getId()] = [$search, $presenter];
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for all registered extensions
|
||||
*
|
||||
* @param $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get($type)
|
||||
{
|
||||
if (!isset($this->searchEngines[$type])) {
|
||||
throw new InvalidArgumentException('Invalid type ' . $type);
|
||||
}
|
||||
|
||||
return $this->searchEngines[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this section is not already defined
|
||||
*
|
||||
* @param $type
|
||||
* @param $id
|
||||
*
|
||||
* @throws EnumerationExistsException
|
||||
*/
|
||||
private function checkIfRegistered($type, $id)
|
||||
{
|
||||
if (isset($this->searchEngines[$type][$id])) {
|
||||
throw new EnumerationExistsException('Already existing search engine ' . $id . ' in ' . $type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace App\CMS\Handlers\Registrators;
|
||||
|
||||
use App\CMS\Enumerations\Exceptions\EnumerationExistsException;
|
||||
use App\CMS\Handlers\Registrators\Items\SettingExtension;
|
||||
|
||||
class SettingsExtensionRegistrator
|
||||
{
|
||||
protected $settingsExtensions;
|
||||
|
||||
function __construct($app)
|
||||
{
|
||||
$this->settingsExtensions = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for registering new settings extensions/parts
|
||||
*
|
||||
* @param $sectionId
|
||||
* @param $sectionName
|
||||
* @param $sectionView
|
||||
*
|
||||
* @throws EnumerationExistsException
|
||||
*/
|
||||
public function register($sectionId, $sectionName, $sectionView)
|
||||
{
|
||||
$this->checkIfRegistered($sectionId);
|
||||
|
||||
// register new extension
|
||||
$this->settingsExtensions[$sectionId] = new SettingExtension($sectionId, $sectionName, $sectionView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers new setting part - only as SettingExtension directly
|
||||
*
|
||||
* @param SettingExtension $extension
|
||||
*/
|
||||
public function registerObject(SettingExtension $extension)
|
||||
{
|
||||
$this->checkIfRegistered($extension->getId());
|
||||
|
||||
$this->settingsExtensions[$extension->getId()] = $extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for all registered extensions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
// sort registered extensions by key
|
||||
ksort($this->settingsExtensions);
|
||||
|
||||
return $this->settingsExtensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this section is not already defined
|
||||
*
|
||||
* @param $sectionId
|
||||
*
|
||||
* @throws EnumerationExistsException
|
||||
*/
|
||||
private function checkIfRegistered($sectionId)
|
||||
{
|
||||
if (isset($this->settingsExtensions[$sectionId])) {
|
||||
throw new EnumerationExistsException('Already existing key ' . $sectionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
namespace App\CMS\Handlers\Registrators;
|
||||
|
||||
use App\CMS\Enumerations\TemplatePaths;
|
||||
use App\CMS\Enumerations\Templates;
|
||||
use App\Models\Template;
|
||||
use App\Models\TemplatePath;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
||||
|
||||
class TemplateRegistrator
|
||||
{
|
||||
/**
|
||||
* @var string absolute path to base directory of registered template paths
|
||||
*/
|
||||
protected $baseDirectory;
|
||||
/**
|
||||
* @var string namespace of registered templates
|
||||
*/
|
||||
protected $namespace;
|
||||
/**
|
||||
* @var string absolute path to currently registered directory
|
||||
* This variable is set in register() method as $baseDirectory/$path
|
||||
*/
|
||||
protected $registeredPath;
|
||||
/**
|
||||
* @var int ID of module
|
||||
*/
|
||||
protected $moduleId;
|
||||
/**
|
||||
* @var array list of all registered template paths
|
||||
*/
|
||||
protected $paths = [];
|
||||
|
||||
/**
|
||||
* Set base path
|
||||
*
|
||||
* @param mixed $baseDirectory
|
||||
*/
|
||||
public function setBaseDirectory($baseDirectory)
|
||||
{
|
||||
$baseDirectory = str_replace('/', DIRECTORY_SEPARATOR, $baseDirectory);
|
||||
$baseDirectory = base_path($baseDirectory);
|
||||
$baseDirectory = rtrim($baseDirectory, DIRECTORY_SEPARATOR);
|
||||
|
||||
if (!is_dir($baseDirectory)) {
|
||||
throw new InvalidArgumentException('Invalid path ' . $baseDirectory);
|
||||
}
|
||||
|
||||
$this->baseDirectory = $baseDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set module ID for registered path
|
||||
*
|
||||
* @param int $moduleId
|
||||
*/
|
||||
public function setModuleId($moduleId)
|
||||
{
|
||||
$this->moduleId = $moduleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set namespace for registered template path
|
||||
*
|
||||
* @param string $namespace
|
||||
*/
|
||||
public function setNamespace($namespace)
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets namespace
|
||||
*/
|
||||
public function unsetNamespace()
|
||||
{
|
||||
$this->namespace = null;
|
||||
$this->setModuleId(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers new template path.
|
||||
* This method registeres given path itself with all subdirectories
|
||||
*
|
||||
* @param $baseDirectory
|
||||
* @param string $path path relatively to root of the project
|
||||
* @param string $name human readable identification of given template path
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function register($baseDirectory, $path, $name)
|
||||
{
|
||||
$this->paths[] = [
|
||||
'namespace' => $this->namespace,
|
||||
'moduleId' => $this->moduleId,
|
||||
'baseDirectory' => $baseDirectory,
|
||||
'path' => $this->baseDirectory . DIRECTORY_SEPARATOR . $path,
|
||||
'name' => $name
|
||||
];
|
||||
$this->unsetNamespace();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save templates into database
|
||||
*/
|
||||
public function saveTemplates()
|
||||
{
|
||||
if (!is_array($this->paths) || empty($this->paths)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->paths as $path) {
|
||||
if (!empty($path['namespace'])) {
|
||||
$this->setNamespace($path['namespace'] . '::');
|
||||
}
|
||||
$this->setModuleId($path['moduleId']);
|
||||
$this->setBaseDirectory($path['baseDirectory']);
|
||||
|
||||
$this->registeredPath = $this->baseDirectory . DIRECTORY_SEPARATOR . $path['path'];
|
||||
// register path
|
||||
$this->registerPath($this->registeredPath, $path['name']);
|
||||
$this->unsetNamespace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get relative path to project root
|
||||
*
|
||||
* @param $directory
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function relativePath($directory)
|
||||
{
|
||||
return trim(str_replace(base_path(), '', $directory), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Crawl subdirectories and register them
|
||||
*
|
||||
* @param string $directory path relative to $baseDirectory variable
|
||||
* @param string $name name of parent directory
|
||||
*/
|
||||
protected function scanForSubdirectories($directory, $name)
|
||||
{
|
||||
$directories = File::directories($directory);
|
||||
|
||||
foreach ($directories as $dir) {
|
||||
$base = basename($dir);
|
||||
$name = preg_replace('/\.(.+)/', '.' . $base, $name);
|
||||
|
||||
$this->registerPath($dir, $name, $directory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers template path and templates into system
|
||||
*
|
||||
* @param string $path relative path to $baseDirectory variable
|
||||
* @param string $name human readable name of this path (used in overview)
|
||||
* @param null|string $parent path to parent directory
|
||||
*/
|
||||
protected function registerPath($path, $name, $parent = null)
|
||||
{
|
||||
// skip custom directories (contains updated templates only)
|
||||
$dirName = basename($path);
|
||||
$ignoredDirectories = [
|
||||
Template::EDITED_TEMPLATES_DIRECTORY,
|
||||
Template::ASSETS_DIRECTORY,
|
||||
Template::MODULES_DIRECTORY
|
||||
];
|
||||
|
||||
if (in_array($dirName, $ignoredDirectories)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// save template path into database
|
||||
$templatePathId = $this->saveTemplatePath($path, $name, $parent);
|
||||
|
||||
// save all templates into database
|
||||
$this->registerTemplates($path, $templatePathId);
|
||||
|
||||
// try to find some subdirectories and other templates
|
||||
$this->scanForSubdirectories($path, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all templates
|
||||
* This method takes all templates in given directory and registers them into database. Templates registered in
|
||||
* this way may be used for further manipulation.
|
||||
*
|
||||
* @param $path
|
||||
* @param $templatePathId
|
||||
*
|
||||
* @throws \App\CMS\Enumerations\Exceptions\EnumerationExistsException
|
||||
*/
|
||||
protected function registerTemplates($path, $templatePathId)
|
||||
{
|
||||
$templates = File::files($path);
|
||||
|
||||
foreach ($templates as $template) {
|
||||
|
||||
// skip all non-template files - we assume that every file with suffix ".blade.php" is Blade template
|
||||
if (!Str::endsWith($template, Template::TEMPLATE_SUFFIX)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// get all templates and template name (path/to/template.blade.php -> path.to.template)
|
||||
$templateName = $this->getTemplateName($template);
|
||||
$registeredTemplates = Templates::getWithTranslations();
|
||||
|
||||
if (!in_array($templateName, $registeredTemplates)) {
|
||||
$params = [
|
||||
'template_name' => $templateName,
|
||||
'name' => $templateName,
|
||||
'path' => $templatePathId,
|
||||
'module_id' => $this->moduleId,
|
||||
'description' => '',
|
||||
'editable' => 1
|
||||
];
|
||||
|
||||
// save template into database
|
||||
$newTemplate = Template::create($params);
|
||||
|
||||
// push template into enumeration - so it will not be registered again
|
||||
Templates::push($newTemplate->id, $templateName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save template into database and return its ID
|
||||
*
|
||||
* @param $path
|
||||
* @param $name
|
||||
* @param null $parent
|
||||
*
|
||||
* @return mixed|null
|
||||
* @throws \App\CMS\Enumerations\Exceptions\EnumerationExistsException
|
||||
*/
|
||||
protected function saveTemplatePath($path, $name, $parent = null)
|
||||
{
|
||||
$registeredPaths = TemplatePaths::getWithTranslations();
|
||||
$relativePath = $this->relativePath($path);
|
||||
|
||||
if (!in_array($relativePath, $registeredPaths)) {
|
||||
$parentId = $this->getId($this->relativePath($parent));
|
||||
|
||||
$params = [
|
||||
'path' => $relativePath,
|
||||
'name' => $name,
|
||||
'namespace' => $this->namespace,
|
||||
'parent_id' => $parentId
|
||||
];
|
||||
|
||||
// save into database
|
||||
$path = TemplatePath::create($params);
|
||||
|
||||
// add to enumeration
|
||||
TemplatePaths::push($path->id, $relativePath);
|
||||
|
||||
return $path->id;
|
||||
}
|
||||
|
||||
return $this->getId($relativePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ID of registered template
|
||||
*
|
||||
* @param $path
|
||||
*
|
||||
* @return null|int null when parent not found, ID of parent otherwise
|
||||
*/
|
||||
private function getId($path)
|
||||
{
|
||||
// get registered paths
|
||||
$paths = TemplatePaths::getWithTranslations();
|
||||
$paths = array_flip($paths);
|
||||
|
||||
if (is_null($path) || !isset($paths[$path])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $paths[$path];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get template name
|
||||
* Converts path to template to corresponding name
|
||||
*
|
||||
* @param $template
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getTemplateName($template)
|
||||
{
|
||||
$templateName = str_replace($this->registeredPath . DIRECTORY_SEPARATOR, '', $template);
|
||||
$templateName = str_replace(DIRECTORY_SEPARATOR, '.', $templateName);
|
||||
$templateName = str_replace(Template::TEMPLATE_SUFFIX, '', $templateName);
|
||||
|
||||
return $this->namespace . $templateName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace App\CMS\Handlers\Registrators;
|
||||
|
||||
use App\CMS\Enumerations\WidgetTypesWithHandlers;
|
||||
use App\Models\Widgets\WidgetType;
|
||||
|
||||
class WidgetRegistrator
|
||||
{
|
||||
protected $registeredWidgets = [];
|
||||
|
||||
|
||||
/**
|
||||
* Method used for registering new widgets
|
||||
*
|
||||
* @param $classname
|
||||
* @param null $templateId
|
||||
* @param null $moduleId
|
||||
*/
|
||||
public function register($classname, $templateId = null, $moduleId = null)
|
||||
{
|
||||
$this->registeredWidgets[] = [
|
||||
'class' => $classname,
|
||||
'templateId' => $templateId,
|
||||
'moduleId' => $moduleId
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Install all non-installed widgets
|
||||
*/
|
||||
public function installWidgets()
|
||||
{
|
||||
$installed = WidgetTypesWithHandlers::getSimple();
|
||||
foreach ($this->registeredWidgets as $registeredWidget) {
|
||||
$classname = $registeredWidget['class'];
|
||||
$templateId = $registeredWidget['templateId'];
|
||||
$moduleId = $registeredWidget['moduleId'];
|
||||
|
||||
// register widget as new widget type
|
||||
if (!in_array($classname, $installed)) {
|
||||
$widgetName = call_user_func($classname . '::getName');
|
||||
$widgetDescription = call_user_func($classname . '::getDescription');
|
||||
|
||||
// create row in widget types
|
||||
WidgetType::create([
|
||||
'handler' => $classname,
|
||||
'name' => $widgetName,
|
||||
'module_id' => $moduleId,
|
||||
'template_id' => $templateId,
|
||||
'description' => $widgetDescription
|
||||
]);
|
||||
|
||||
// install widget (if neccessary)
|
||||
call_user_func($classname . '::install');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user