Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
d673c13a
Unverified
Commit
d673c13a
authored
Sep 13, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Creating new routes to store a page and fetch page templates
parent
43a25b85
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
0 deletions
+126
-0
PageTemplatesController.php
...les/Page/Http/Controllers/Api/PageTemplatesController.php
+120
-0
apiRoutes.php
Modules/Page/Http/apiRoutes.php
+6
-0
No files found.
Modules/Page/Http/Controllers/Api/PageTemplatesController.php
0 → 100644
View file @
d673c13a
<?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
);
}
}
Modules/Page/Http/apiRoutes.php
View file @
d673c13a
...
...
@@ -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'
);
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment