1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 21:56:33 +02:00

Flextype Core: New Snippets API

This commit is contained in:
Awilum
2019-02-20 13:49:29 +03:00
parent da4573fc35
commit 8c5fd02d08

View File

@@ -19,52 +19,138 @@ class Snippets
/**
* Get snippet
*
* Snippets::get('snippet-name');
* Snippets::fetch('snippet-name');
*
* @access public
* @param string $snippet_name Snippet name
* @return string|bool Returns the contents of the output buffer and end output buffering.
* If output buffering isn't active then FALSE is returned.
*/
public static function get(string $snippet_name)
public static function fetch(string $snippet)
{
$vars = [];
$vars['get'] = $snippet_name;
$vars['fetch'] = $snippet;
return Snippets::_snippet($vars);
return Snippets::_fetch_snippet($vars);
}
/**
* _snippet
* Rename snippet.
*
* Snippets::get('snippet-name');
* Snippets::get('snippetname', ['message' => 'Hello World']);
* @access public
* @param string $snippet Snippet
* @param string $new_snippet New snippet
* @return bool True on success, false on failure.
*/
public static function rename(string $snippet, string $new_snippet) : bool
{
return rename($snippet, $new_snippet);
}
/**
* Update Snippet
*
* @access public
* @param string $snippet Snippet
* @param string $data Data
* @return bool True on success, false on failure.
*/
public static function update(string $snippet, string $data) : bool
{
$snippet_file = PATH['snippets'] . '/' . $snippet . '.php';
if (Filesystem::has($snippet_file)) {
return Filesystem::write($snippet, $data);
} else {
return false;
}
}
/**
* Create snippet
*
* @access public
* @param string $snippet Snippet
* @param string $data Data
* @return bool True on success, false on failure.
*/
public static function create(string $snippet, string $data) : bool
{
$snippet_file = PATH['snippets'] . '/' . $snippet . '.php';
// Check if new entry file exists
if (!Filesystem::has($snippet_file)) {
return Filesystem::write($snippet_file, $data);
} else {
return false;
}
}
/**
* Delete snippet.
*
* @access public
* @param string $snippet Snippet
* @return bool True on success, false on failure.
*/
public static function delete(string $snippet) : bool
{
return Filesystem::delete(PATH['snippets'] . '/' . $entry . '.php');
}
/**
* Copy snippet
*
* @access public
* @param string $snippet Snippet
* @param string $new_snippet New snippet
* @return bool True on success, false on failure.
*/
public static function copy(string $snippet, string $new_snippet) : bool
{
return Filesystem::copy($snippet, $new_snippet, false);
}
/**
* Check whether snippet exists.
*
* @access public
* @param string $snippet Snippet
* @return bool True on success, false on failure.
*/
public static function has(string $snippet) : bool
{
return Filesystem::has(PATH['snippets'] . '/' . $snippet . '.php');
}
/**
* Helper private method _fetch_snippet
*
* @access private
* @param array $vars Vars
* @return string|bool Returns the contents of the output buffer and end output buffering.
* If output buffering isn't active then FALSE is returned.
*/
private static function _snippet(array $vars) {
private static function _fetch_snippet(array $vars) {
// Extracst attributes
extract($vars);
// Get snippet name
$name = (isset($get)) ? (string) $get : '';
$name = (isset($fetch)) ? (string) $fetch : '';
// Define snippet path
$snippet_path = PATH['snippets'] . '/' . $name . '.php';
$snippet_file = PATH['snippets'] . '/' . $name . '.php';
// Process snippet
if (Filesystem::has($snippet_path)) {
if (Filesystem::has($snippet_file)) {
// Turn on output buffering
ob_start();
// Include view file
include $snippet_path;
include $snippet_file;
// Output...
return ob_get_clean();