Creating new routes to store a page and fetch page templates

parent 43a25b85
<?php
namespace Modules\Page\Http\Controllers\Api;
use Illuminate\Routing\Controller;
use Modules\Page\Services\FinderService;
use Modules\Workshop\Manager\ThemeManager;
class PageTemplatesController extends Controller
{
/**
* @var ThemeManager
*/
private $themeManager;
/**
* @var FinderService
*/
private $finder;
public function __construct(ThemeManager $themeManager, FinderService $finder)
{
$this->themeManager = $themeManager;
$this->finder = $finder;
}
public function __invoke()
{
return response()->json($this->getTemplates());
}
private function getTemplates()
{
$path = $this->getCurrentThemeBasePath();
$templates = [];
foreach ($this->finder->excluding(config('asgard.page.config.template-ignored-directories', []))->allFiles($path . '/views') as $template) {
$relativePath = $template->getRelativePath();
$templateName = $this->getTemplateName($template);
$file = $this->removeExtensionsFromFilename($template);
if ($this->hasSubdirectory($relativePath)) {
$templates[str_replace('/', '.', $relativePath) . '.' . $file] = $templateName;
} else {
$templates[$file] = $templateName;
}
}
return $templates;
}
/**
* Get the base path of the current theme.
*
* @return string
*/
private function getCurrentThemeBasePath()
{
return $this->themeManager->find(setting('core::template'))->getPath();
}
/**
* Read template name defined in comments.
*
* @param $template
*
* @return string
*/
private function getTemplateName($template)
{
preg_match("/{{-- Template: (.*) --}}/", $template->getContents(), $templateName);
if (count($templateName) > 1) {
return $templateName[1];
}
return $this->getDefaultTemplateName($template);
}
/**
* If the template name is not defined in comments, build a default.
*
* @param $template
*
* @return mixed
*/
private function getDefaultTemplateName($template)
{
$relativePath = $template->getRelativePath();
$fileName = $this->removeExtensionsFromFilename($template);
return $this->hasSubdirectory($relativePath) ? $relativePath . '/' . $fileName : $fileName;
}
/**
* Remove the extension from the filename.
*
* @param $template
*
* @return mixed
*/
private function removeExtensionsFromFilename($template)
{
return str_replace('.blade.php', '', $template->getFilename());
}
/**
* Check if the relative path is not empty (meaning the template is in a directory).
*
* @param $relativePath
*
* @return bool
*/
private function hasSubdirectory($relativePath)
{
return ! empty($relativePath);
}
}
......@@ -18,4 +18,10 @@ $router->group(['prefix' => '/page'], function (Router $router) {
'uses' => 'PageController@destroy',
'middleware' => 'token-can:page.pages.destroy',
]);
$router->post('pages', [
'as' => 'api.page.page.store',
'uses' => 'PageController@store',
'middleware' => 'token-can:page.pages.create',
]);
$router->get('templates', 'PageTemplatesController')->name('api.page.page-templates.index');
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment