diff --git a/CHANGELOG.md b/CHANGELOG.md index a9c0be5..905203c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,24 @@ + +# [1.7.0](https://github.com/flextype-plugins/twig/compare/v1.6.0...v1.7.0) (2020-12-30) + +### Features + +* **core** update code base for new Flextype 0.9.14 +* **core** Moving to PHP 7.4 +* **core** use new TWIG Plugin 1.7.0 +* **extension** new Constants Extension +* **extension** new Filesystem Extension +* **extension** new Flextype Extension +* **extension** new Filters Extension + +New docs on the way here https://github.com/flextype-plugins/twig/ + +BREAKING CHANGES + +* in the templates use `flextype.registry` instead of `registry` +* in the templates use `flextype.entries` instead of `entries` +* in the templates use `flextype.media` instead of `media_*` + # [1.6.0](https://github.com/flextype-plugins/twig/compare/v1.5.1...v1.6.0) (2020-12-20) diff --git a/README.md b/README.md index 68baf67..aa0e5a3 100755 --- a/README.md +++ b/README.md @@ -1,18 +1,25 @@

Twig Plugin for Flextype

-Version License Total downloads Flextype Discord +Version License Total downloads Flextype Discord

Twig plugin to present Twig template engine for Flextype. +* [Dependencies](#dependencies) +* [Installation](#installation) +* [Global Variables](#global-variables) +* [Functions](#functions) +* [Filters](#filters) +* [License](#license) + ## Dependencies The following dependencies need to be installed for Twig Plugin. | Item | Version | Download | |---|---|---| -| [flextype](https://github.com/flextype/flextype) | 0.9.13 | [download](https://github.com/flextype/flextype/releases) | +| [flextype](https://github.com/flextype/flextype) | 0.9.14 | [download](https://github.com/flextype/flextype/releases) | ## Installation @@ -20,331 +27,180 @@ The following dependencies need to be installed for Twig Plugin. 2. Create new folder `/project/plugins/twig` 3. Download Twig Plugin and unzip plugin content to the folder `/project/plugins/twig` -## Documentation - -### Twig Filters & Functions - -Twig already provides an extensive list of [filters, functions, and tags](https://twig.symfony.com/doc/2.x/), Flextype also provides a selection of useful additions to make the process of theming easier. - -#### Flextype Twig Filters +### Introduction -Twig filters are applied to Twig variables by using the `|` character followed by the filter name. Parameters can be passed in just like Twig functions using parenthesis. +Twig is a modern template engine for PHP. -##### shortcode +* **Fast**: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum. +* **Secure**: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design. +* **Flexible**: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL. -Parse shortcode +Twig templates are HTML files that are sprinkled with bits of Twig code. When Twig loads a template, the first thing it will do is separate the raw HTML code from the Twig code. The raw HTML code will be output to the browser without any tampering. -Usage: +All Twig code follows a basic pattern that separates it from the surrounding HTML. At its outer edges you will find left and right curly braces {{ and }}, coupled with another character that signifies what type of Twig code it is. These sets of characters are called “delimiters”. -```twig -{{ '[b]Bold text[/b]'|shortcode }} -``` +There are three types of delimiters that Twig looks out for: -Result: +`{#` – [Comments](#comments) +`{%` – [Tags](#tags) +`{{` – [Print statements](#print-statements) -**Bold text** +#### Comments -##### markdown +Twig comments are wrapped in {# and #} delimiters. You can use them to leave little notes for yourself in the code. -Parse markdown +They are similar to HTML comments in that they won’t show up as rendered text in the browser. The difference is that they will never make it into the HTML source in the first place. -Usage: markdown - -```twig -{{ '**Bold text**'|markdown }} ``` - -Result: - -**Bold text** - -##### tr - -Translate text - -Usage: -```twig -{{ 'site_powered_by_flextype'|tr }} + +{# This won’t! #} ``` -Result: +#### Tags -Build fast, flexible, easier to manage websites with -Flextype. +Twig tags are wrapped in {% and %} delimiters, and are used to define the logic of your template, such as conditionals, loops, variable definitions, template includes, and other things. -Multiple filters can be chained. The output of one filter is applied to the next. +The syntax within the {% and %} delimiters varies from tag to tag, but they will always start with the same thing: the name of the tag. -Usage: -```twig -{{ '[b]Bold text[/b] *Italic text*'|shortcode|markdown }} -``` - -Result: - -**Bold text** *Italic text* - -#### Flextype Twig Functions +#### Print Statements -Twig functions are called directly with any parameters being passed in via parenthesis. +To output additional HTML code dynamically, use a print statement. They are wrapped in {{ and }} delimiters, and you can put just about anything inside them, as long as it can be treated as a string. -##### yaml_decode - -Decode valid yaml string into array - -Usage: -```twig -{{ yaml_decode('title: Hello World!').title }} ``` - -Result: -```twig -Hello World! +

Hi, {{ entry.title }}

``` -##### yaml_encode +Don’t place a print statement (or any other Twig code) within another print statement. [See Combining Strings to learn how to combine strings with other expressions](#combining-strings). -Encode array into valid yaml string - -Usage: -```twig -{{ yaml_encode({'title': 'Hello World!'})}} -``` - -Result: -```yaml -title: 'Hello World!' -``` +#### Auto-escaping -##### json_decode +Most of the time, print statements will automatically HTML-encode the content before actually outputting it (called auto-escaping), which helps defend against cross-site scripting (XSS) vulnerabilities. -Decode valid json string into array +For example, let’s say you have a search results page, where the search query is defined by a q query string parameter, and in the event that there are no results, you want to output a message to the user that includes the query: -Usage: -```twig -{{ json_decode('{"title": "Hello World!"}').title }} -``` - -Result: -```twig -Hello World! -``` - -##### json_encode - -Encode array into valid json string - -Usage: -```twig -{{ json_encode({'title': 'Hello World!'})}} -``` - -Result: -```json -{"title": "Hello World!"} -``` - -##### tr or __ - -Translate string - -Usage: -```twig -{{ tr('site_powered_by_flextype') }} -{{ __('site_powered_by_flextype') }} -``` - -Result: - -Build fast, flexible, easier to manage websites with -Flextype. - -##### filesystem_list_contents - -List contents of a directory - -Usage: - -```twig -{% set media = filesystem_list_contents(PATH_UPLOADS ~ '/' ~ entry.id) %} ``` +{% set query = request.getQueryParam().q %} -Result: - -array (media) +{% set entries = flextype.entries.fetch('blog', {'collection': true}) + .where('title', 'contains', query) + .all() %} -##### filesystem_has - -Check whether a file exists - -Usage: -```twig -{% if (filesystem_has(PATH_PROJECT ~ '/media/' ~ entry.id ~ '/about.md')) %} - Show something... +{% if entries %} +

Search Results

+ +{% else %} +

Sorry, no results for {{ query }} were found.

{% endif %} ``` -##### filesystem_read +If it weren’t for auto-escaping, a search for would result in this HTML: -Read a file - -Usage: -```twig -{{ filesystem_read(PATH_PROJECT ~ '/media/' ~ entry.id ~ '/about.md') }} ``` - -Result: - -``` - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. -``` - -##### filesystem_ext - -Get file extension - -Usage: -```twig -{{ filesystem_ext(PATH_PROJECT ~ '/media/' ~ entry.id ~ '/about.md') }} +

Sorry, no results for .

``` -Result: - -.md - -##### filesystem_basename - -Get filename - -Usage: +Which would cause JavaScript to execute on the page, even though it wasn’t part of the original Twig template. But thanks to auto-escaping, you’d actually end up with this HTML: -```twig -{{ filesystem_basename(PATH_PROJECT ~ '/media/' ~ entry.id ~ '/about.md') }} ``` - -Result: - -entry - -##### path_for - -Returns the URL for a given route. - -Usage: -```twig -{{ path_for('profile') }} +

Sorry, no results for <script>alert('Danger!!!')</script>.

``` -##### base_url +There are two cases where print statements will output content directly, without auto-escaping it first: -Returns the Uri object's base URL. +* When the content is deemed safe by the last tag or function that was called within the print statement (such as the markdown filter). +* When you explicitly mark the content as safe using a raw filter. -Usage: -```twig -{{ base_url() }} -``` +#### Manual escaping -##### url +There are times where you may need to work with both trusted and untrusted content together. For example, let’s say you want to output user-supplied content as Markdown, but you want to ensure they haven’t put anything nefarious in there first. -Returns the Uri object's App URL. +To do that, you could explicitly encode all HTML within the user-supplied content using the escape (opens new window) filter, before passing it to the markdown filter: -Usage: -```twig -{{ url() }} -``` - -##### is_current_path - -Returns true is the provided route name and parameters are valid for the current path. -```twig -{% if is_current_path('profile') %} - Show profile page -{% endif %} ``` - -##### current_path - -Renders the current path, with or without the query string. -```twig -{{ current_path() }} +{# Escape any HTML in the Content field, then format as Markdown #} +{{ entry.content|escape|markdown }} ``` -### Twig Variables +#### Resources -When you are working with twig templates, for e.g. when you designing a theme, Twig Plugin gives you access to all sorts of objects and variables available in Flextype within your Twig templates. The Twig templating framework provides powerful ways to read and manipulate these objects and variables. +* [Official Twig Documentation](http://twig.sensiolabs.org/documentation) +* [Twig for Template Designers](http://twig.sensiolabs.org/doc/templates.html) +* [Twig for Developers](http://twig.sensiolabs.org/doc/api.html) +* [6 Minute Video Introduction to Twig](http://www.dev-metal.com/6min-video-introduction-twig-php-templating-engine/) +* [Introduction to Twig](http://www.slideshare.net/markstory/introduction-to-twig) -#### Core Objects +### Global Variables -There are several **core objects** that are available to a Twig template, and each object has a set of **variables** and **functions**. +The following Flextype Twig Global variables are available in Flextype Twig Templates: -#### registry +| Variable | Description | +|---|---| +| _self | The current template name. | +| _context | The currently-defined variables. | +| _charset | The current charset. | +| PATH_PROJECT | Project path (without trailing slash). | +| PHP_VERSION | PHP Version. | +| flextype | Returns Flextype object. | +<<<<<<< HEAD Registry stored all flextype, themes and plugins settings.
You can access and manipulate Flextype registry via the registry object. +======= +#### PATH_PROJECT +>>>>>>> dev -Usage: +Project path (without trailing slash). +<<<<<<< HEAD ```twig {{ registry.dump() }} {{ registry.set() }} {{ registry.has() }} {{ registry.get() }} +======= +>>>>>>> dev ``` - -##### entry - -Because Flextype is built using the structure defined in the `/project/entries/` folder, each entry is represented by a entry object. - -The entry object is probably the most important object you will work with as it contains all the information about the current page you are currently on. - -Usage: - -```twig -{{ entry.title }} {# returns the current entry title #} -``` - -##### entries - -Fetch single entry - -Usage: - -```twig -{% set about_entry = entries.fetch('about') %} +{{ PATH_PROJECT }} ``` -Fetch collection of entries +#### PHP_VERSION -Usage: +PHP Version. -```twig -{% set posts = entries.fetch('blog', {'collection': true}) %} ``` - -##### emitter - -Emitting events - -Usage: -```twig -{% do emitter.emit('onThemeHeader') %} +{{ PHP_VERSION }} ``` -Emitting events in batches - -Usage: - -```twig -{% do emitter.emitBatch({'onThemeHeader', 'onSomeOtherEvent'}) %} -``` +#### flextype +<<<<<<< HEAD ##### arrays +======= +Returns Flextype object, which provides access points to various helper functions and objects for templates. +>>>>>>> dev -Contains methods that can be useful when working with arrays. +| Objects | Available Methods | +|---|---| +| `flextype.entries` | [fetch()](https://docs.flextype.org/en/core/entries#methods-fetch) +| `flextype.media.files` | [fetch()](https://docs.flextype.org/en/core/media#methods-files-fetch) [has()](https://docs.flextype.org/en/core/media#methods-files-has) | +| `flextype.media.files.meta` | [getFileMetaLocation()](https://docs.flextype.org/en/core/media#methods-files-meta-getFileMetaLocation) | +| `flextype.media.folders` | [fetch()](https://docs.flextype.org/en/core/media#methods-folders-fetch) [getDirectoryLocation()](https://docs.flextype.org/en/core/media#methods-folders-getDirLocation) | +| `flextype.media.folders.meta` | [getDirMetaLocation()](https://docs.flextype.org/en/core/media#methods-folders-meta-getDirMetaLocation) | +| `flextype.registry` | [all methods available](https://docs.flextype.org/en/core/registry) | +| `flextype.parsers` | [all methods available](https://docs.flextype.org/en/core/parsers) | +| `flextype.serializers` | [all methods available](https://docs.flextype.org/en/core/serializers) | +| `flextype.cache` | [all methods available](https://www.phpfastcache.com) | +| `flextype.emitter` | [all methods available](https://event.thephpleague.com/2.0/) | -Sorts a multi-dimensional array by a certain column +### Functions -Usage: +The following Flextype Twig Functions are available in Flextype Twig Templates: +<<<<<<< HEAD ```twig {% set new_array = arrays(old.array).sortBy('title') %} ``` @@ -355,6 +211,22 @@ Usage: {{ PATH_PROJECT }} {# Returns the path to the project directory (without trailing slash). #} {{ PHP_VERSION }} {# Returns the php version #} ``` +| Variable | Description | +|---|---| +| arrays | Returns a new arrays object from the given elements. | +| strings | Returns a new strings object from the given string. | +| filesystem | Returns a new filesystem object from the given string. | +| url | Returns application URL. | + +### Filters + +The following Flextype Twig Filters are available in Flextype Twig Templates: + +| Variable | Description | +|---|---| +| __ | Translate string | +| shortcode | Shortcode parser | +| markdown | Markdown parser | ## LICENSE [The MIT License (MIT)](https://github.com/flextype-plugins/twig/blob/master/LICENSE.txt) diff --git a/composer.json b/composer.json index 8e1fcc1..c439f5f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "issues": "https://github.com/flextype-plugins/twig/issues" }, "require": { - "php": ">=7.3.0", + "php": ">=7.4.0", "slim/twig-view": "~2.5.0", "slim/csrf": "~0.8.3", "slim/flash": "~0.4.0", @@ -27,7 +27,7 @@ "apcu-autoloader": true, "optimize-autoloader": true, "platform": { - "php": "7.3.0" + "php": "7.4.0" } }, "autoload": { diff --git a/dependencies.php b/dependencies.php index 76d9bf8..cc3517c 100644 --- a/dependencies.php +++ b/dependencies.php @@ -22,52 +22,41 @@ /** * Add CSRF (cross-site request forgery) protection service to Flextype container */ -flextype()->container()['csrf'] = static function (){ - return new Guard(); -}; +flextype()->container()['csrf'] = fn() => new Guard(); /** * Add Twig service to Flextype container */ -flextype()->container()['twig'] = static function () { - - // Get twig settings - $twigSettings = [ - 'auto_reload' => flextype('registry')->get('plugins.twig.settings.auto_reload'), - 'cache' => flextype('registry')->get('plugins.twig.settings.cache') ? PATH['tmp'] . '/twig' : false, - 'debug' => flextype('registry')->get('plugins.twig.settings.debug'), - 'charset' => flextype('registry')->get('plugins.twig.settings.charset') - ]; +flextype()->container()['twig'] = function () { // Create Twig View - $twig = new Twig(PATH['project'], $twigSettings); - - // Instantiate - $router = flextype('router'); - $uri = Uri::createFromEnvironment(new Environment($_SERVER)); + $twig = new Twig(PATH['project'], + ['auto_reload' => flextype('registry')->get('plugins.twig.settings.auto_reload'), + 'cache' => flextype('registry')->get('plugins.twig.settings.cache') ? PATH['tmp'] . '/twig' : false, + 'debug' => flextype('registry')->get('plugins.twig.settings.debug'), + 'charset' => flextype('registry')->get('plugins.twig.settings.charset')]); // Add Twig Extension - $twig->addExtension(new TwigExtension($router, $uri)); + $twig->addExtension(new TwigExtension(flextype('router'), + Uri::createFromEnvironment(new Environment($_SERVER)))); // Add Twig Debug Extension $twig->addExtension(new DebugExtension()); // Load Flextype Twig extensions from directory /flextype/twig/ based on settings.twig.extensions array - $twig_extensions = flextype('registry')->get('plugins.twig.settings.extensions'); + $twigExtensions = flextype('registry')->get('plugins.twig.settings.extensions'); - foreach ($twig_extensions as $twig_extension) { - $twig_extension_class_name = $twig_extension . 'TwigExtension'; - $twig_extension_class_name_with_namespace = 'Flextype\\Plugin\\Twig\\Twig\\' . $twig_extension . 'TwigExtension'; + foreach ($twigExtensions as $twigExtension) { + $twigExtensionClassName = $twigExtension . 'TwigExtension'; + $twigExtensionClassNameWithNamespace = 'Flextype\\Plugin\\Twig\\Twig\\' . $twigExtension . 'TwigExtension'; - if (file_exists(ROOT_DIR . '/project/plugins/twig/twig/' . $twig_extension_class_name . '.php')) { + if (file_exists(ROOT_DIR . '/project/plugins/twig/twig/' . $twigExtensionClassName . '.php')) { - if ($twig_extension == 'Flash') { - flextype()->container()['flash'] = static function () { - return new Messages(); - }; + if ($twigExtension == 'Flextype') { + flextype()->container()['flash'] = fn() => new Messages(); } - $twig->addExtension(new $twig_extension_class_name_with_namespace()); + $twig->addExtension(new $twigExtensionClassNameWithNamespace()); } } diff --git a/plugin.php b/plugin.php new file mode 100644 index 0000000..73b6691 --- /dev/null +++ b/plugin.php @@ -0,0 +1,7 @@ + - */ - function arrays($items = []): Arrays - { - return Arrays::create($items); - } } diff --git a/twig/CacheTwigExtension.php b/twig/CacheTwigExtension.php deleted file mode 100644 index 2058a62..0000000 --- a/twig/CacheTwigExtension.php +++ /dev/null @@ -1,34 +0,0 @@ - flextype('cache'), - ]; - } -} diff --git a/twig/GlobalVarsTwigExtension.php b/twig/ConstantsTwigExtension.php similarity index 69% rename from twig/GlobalVarsTwigExtension.php rename to twig/ConstantsTwigExtension.php index 9193a30..92d720c 100644 --- a/twig/GlobalVarsTwigExtension.php +++ b/twig/ConstantsTwigExtension.php @@ -13,16 +13,8 @@ use Twig\Extension\GlobalsInterface; use const PHP_VERSION; -class GlobalVarsTwigExtension extends AbstractExtension implements GlobalsInterface +class ConstantsTwigExtension extends AbstractExtension implements GlobalsInterface { - /** - * Constructor - */ - public function __construct() - { - - } - /** * Register Global variables in an extension */ @@ -30,7 +22,7 @@ public function getGlobals() : array { return [ 'PATH_PROJECT' => PATH['project'], - 'PHP_VERSION' => PHP_VERSION + 'PHP_VERSION' => PHP_VERSION, ]; } } diff --git a/twig/CsrfTwigExtension.php b/twig/CsrfTwigExtension.php index 60697bb..aab3951 100644 --- a/twig/CsrfTwigExtension.php +++ b/twig/CsrfTwigExtension.php @@ -14,14 +14,6 @@ class CsrfTwigExtension extends AbstractExtension implements GlobalsInterface { - /** - * Constructor - */ - public function __construct() - { - - } - /** * Register Global variables in an extension */ diff --git a/twig/EmitterTwigExtension.php b/twig/EmitterTwigExtension.php deleted file mode 100644 index 23305e0..0000000 --- a/twig/EmitterTwigExtension.php +++ /dev/null @@ -1,66 +0,0 @@ - new EmitterTwig(), - ]; - } -} - -class EmitterTwig -{ - /** - * Flextype Application - */ - - - /** - * Constructor - */ - public function __construct() - { - - } - - /** - * Emitting event - */ - public function emit($event) - { - return flextype('emitter')->emit($event); - } - - /** - * Emitting events in batches - */ - public function emitBatch(array $events) - { - return flextype('emitter')->emitBatch($events); - } -} diff --git a/twig/EntriesTwigExtension.php b/twig/EntriesTwigExtension.php deleted file mode 100644 index bd56eb1..0000000 --- a/twig/EntriesTwigExtension.php +++ /dev/null @@ -1,49 +0,0 @@ - new EntriesTwig(), - ]; - } -} - -class EntriesTwig -{ - /** - * Flextype Application - */ - - /** - * Constructor - */ - public function __construct() - { - - } - - /** - * Fetch. - */ - public function fetch(string $id, array $options = []) - { - return flextype('entries')->fetch($id, $options); - } -} diff --git a/twig/FilesystemTwigExtension.php b/twig/FilesystemTwigExtension.php index 128d6cf..db5362a 100644 --- a/twig/FilesystemTwigExtension.php +++ b/twig/FilesystemTwigExtension.php @@ -9,11 +9,10 @@ namespace Flextype\Plugin\Twig\Twig; -use Flextype\Component\Filesystem\Filesystem; use Twig\Extension\AbstractExtension; -use function basename; -use function strrchr; -use function substr; +use Twig\Extension\GlobalsInterface; +use Atomastic\Macroable\Macroable; +use Symfony\Component\Finder\Finder; class FilesystemTwigExtension extends AbstractExtension { @@ -25,36 +24,324 @@ class FilesystemTwigExtension extends AbstractExtension public function getFunctions() : array { return [ - new \Twig\TwigFunction('filesystem_list_contents', [$this, 'list_contents']), - new \Twig\TwigFunction('filesystem_has', [$this, 'has']), - new \Twig\TwigFunction('filesystem_read', [$this, 'read']), - new \Twig\TwigFunction('filesystem_ext', [$this, 'ext']), - new \Twig\TwigFunction('filesystem_basename', [$this, 'basename']), + new \Twig\TwigFunction('filesystem', function() { return new FilesystemTwig(); }), ]; } +} + +class FilesystemTwig +{ + use Macroable; + + /** + * Create a File instance. + */ + public function file($path): FilesystemFileTwig + { + return new FilesystemFileTwig($path); + } + + /** + * Create a Directory instance. + */ + public function directory($path): FilesystemDirectoryTwig + { + return new FilesystemDirectoryTwig($path); + } + + /** + * Create a Finder instance. + */ + public function find(): Finder + { + return filesystem()->find(); + } + + /** + * Determine if the given path is a stream path. + * + * @param string $path Path to check. + * + * @return bool Returns TRUE if the given path is stream path, FALSE otherwise. + */ + public function isStream(string $path): bool + { + return filesystem()->isStream($path); + } + + /** + * Determine if the given path is absolute path. + * + * @param string $path Path to check. + * + * @return bool Returns TRUE if the given path is absolute path, FALSE otherwise. + */ + public function isAbsolute(string $path): bool + { + return filesystem()->isAbsolute($path); + } + + /** + * Determine if the given path is a Windows path. + * + * @param string $path Path to check. + * + * @return bool true if windows path, false otherwise + */ + public function isWindowsPath(string $path): bool + { + return filesystem()->isWindowsPath($path); + } + + /** + * Find path names matching a given pattern. + * + * @param string $pattern The pattern. + * @param int $flags Valid flags. + * + * @return array Returns an array containing the matched files/directories, an empty array if no file matched. + */ + public function glob(string $pattern, int $flags = 0): array + { + return filesystem()->glob($pattern, $flags); + } +} + +class FilesystemFileTwig +{ + use Macroable; + + /** + * Path property + * + * Current file absolute path + * + * @var string|null + */ + public $path; + + /** + * Constructor + * + * @param string $path Path to file. + */ + public function __construct(string $path) + { + $this->path = $path; + } + + /** + * Get the contents of a file. + * + * @return string|false The file contents or false on failure. + */ + public function get() + { + return filesystem()->file($this->path)->get(); + } + + /** + * Checks the existence of file and returns false if any of them is missing. + * + * @return bool Returns true or false if any of them is missing. + */ + public function exists(): bool + { + return filesystem()->file($this->path)->exists(); + } + + /** + * Get the file's last modification time. + * + * @return int Returns the time the file was last modified. + */ + public function lastModified(): int + { + return filesystem()->file($this->path)->lastModified(); + } + + /** + * Get the file's last access time. + * + * @return int Returns the time the file was last assecc. + */ + public function lastAccess(): int + { + return filesystem()->file($this->path)->lastAccess(); + } + + /** + * Get the mime-type of a given file. + * + * @return string The mime-type of a given file. + */ + public function mimeType(): string + { + return filesystem()->file($this->path)->mimeType(); + } + + /** + * Get the file type of a given file. + * + * @return string The file type of a given file. + */ + public function type(): string + { + return filesystem()->file($this->path)->type(); + } + + /** + * Get the file extension from a file path. + * + * @return string The extension of a given file. + */ + public function extension(): string + { + return filesystem()->file($this->path)->extension(); + } - public function list_contents(string $directory = '', bool $recursive = false) + /** + * Get the trailing name component from a file path. + * + * @return string The trailing name of a given file. + */ + public function basename(): string { - return Filesystem::listContents($directory, $recursive); + return filesystem()->file($this->path)->basename(); } - public function has($path) + /** + * Get the file name from a file path. + * + * @return string The file name of a given file. + */ + public function name(): string { - return Filesystem::has($path); + return filesystem()->file($this->path)->name(); } - public function read($path) + /** + * Return current path. + * + * @return string|null Current path + */ + public function path(): ?string { - return Filesystem::read($path); + return filesystem()->file($this->path)->path(); } - public function ext($file) + /** + * Gets file size in bytes. + * + * @return int Returns the size of the file in bytes. + */ + public function size(): int + { + return filesystem()->file($this->path)->size(); + } + + /** + * Get the MD5 hash of the file at the given path. + * + * @return string Returns a string on success, FALSE otherwise. + */ + public function hash(): string + { + return filesystem()->file($this->path)->hash(); + } + + /** + * Determine if the given path is readable. + * + * @return bool Returns TRUE if the given path exists and is readable, FALSE otherwise. + */ + public function isReadable(): bool + { + return filesystem()->file($this->path)->isReadable(); + } + + /** + * Determine if the given path is writable. + * + * @return bool Returns TRUE if the given path exists and is writable, FALSE otherwise. + */ + public function isWritable(): bool { - return substr(strrchr($file, '.'), 1); + return filesystem()->file($this->path)->isWritable(); } - public function basename($value, $suffix = '') + /** + * Determine if the given path is a regular file. + * + * @return bool Returns TRUE if the filename exists and is a regular file, FALSE otherwise. + */ + public function isFile(): bool + { + return filesystem()->file($this->path)->isFile(); + } +} + + +class FilesystemDirectoryTwig +{ + use Macroable; + + /** + * Path property + * + * Current directory path. + * + * @var string|null + */ + public $path; + + /** + * Constructor + * + * @param string $path Path to directory. + */ + public function __construct(string $path) + { + $this->path = $path; + } + + /** + * Checks the existence of directory and returns false if any of them is missing. + * + * @return bool Returns true or false if any of them is missing. + */ + public function exists(): bool + { + return filesystem()->directory($this->path)->exists(); + } + + /** + * Gets size of a given directory in bytes. + * + * @return int Returns the size of the directory in bytes. + */ + public function size(): int + { + return filesystem()->directory($this->path)->size(); + } + + /** + * Determine if the given path is a directory. + * + * @return bool Returns TRUE if the given path exists and is a directory, FALSE otherwise. + */ + public function isDirectory(): bool + { + return filesystem()->directory($this->path)->isDirectory(); + } + + /** + * Return current path. + * + * @return string|null Current path + */ + public function path(): ?string { - return basename($value, $suffix); + return filesystem()->directory($this->path)->path(); } } diff --git a/twig/ShortcodeTwigExtension.php b/twig/FiltersTwigExtension.php similarity index 58% rename from twig/ShortcodeTwigExtension.php rename to twig/FiltersTwigExtension.php index 279cde0..28ef1b0 100644 --- a/twig/ShortcodeTwigExtension.php +++ b/twig/FiltersTwigExtension.php @@ -3,7 +3,7 @@ declare(strict_types=1); /** - * Flextype (http://flextype.org) + * Flextype (https://flextype.org) * Founded by Sergey Romanenko and maintained by Flextype Community. */ @@ -11,16 +11,8 @@ use Twig\Extension\AbstractExtension; -class ShortcodeTwigExtension extends AbstractExtension +class FiltersTwigExtension extends AbstractExtension { - /** - * Constructor - */ - public function __construct() - { - - } - /** * Returns a list of filters to add to the existing list. * @@ -30,18 +22,17 @@ public function getFilters() : array { return [ new \Twig\TwigFilter('shortcode', [$this, 'shortcode']), + new \Twig\TwigFilter('markdown', [$this, 'markdown']), ]; } - /** - * Shorcode process - */ public function shortcode($value) : string { - if (!empty($value)) { - return flextype('parsers')->shortcode()->process($value); - } + return !empty($value) ? flextype('parsers')->shortcode()->process($value) : ''; + } - return ''; + public function markdown($value) : string + { + return !empty($value) ? flextype('parsers')->markdown()->process($value) : ''; } } diff --git a/twig/FlashTwigExtension.php b/twig/FlashTwigExtension.php deleted file mode 100644 index bfb7ea8..0000000 --- a/twig/FlashTwigExtension.php +++ /dev/null @@ -1,50 +0,0 @@ -getMessage($key); - } - - return flextype('flash')->getMessages(); - } -} diff --git a/twig/FlextypeTwigExtension.php b/twig/FlextypeTwigExtension.php new file mode 100644 index 0000000..8915fed --- /dev/null +++ b/twig/FlextypeTwigExtension.php @@ -0,0 +1,255 @@ + new FlextypeTwig(), + ]; + } +} + +class FlextypeTwig +{ + use Macroable; + + public function entries() + { + return new EntriesTwig(); + } + + public function media() + { + return new MediaTwig(); + } + + public function registry() + { + return flextype('registry'); + } + + public function parsers() + { + return flextype('parsers'); + } + + public function serializers() + { + return flextype('serializers'); + } + + public function cache() + { + return flextype('cache'); + } + + public function emitter() + { + return flextype('emitter'); + } + + public function slugify() + { + return flextype('slugify'); + } + + public function flash() + { + return flextype('flash'); + } +} + +class EntriesTwig +{ + use Macroable; + + /** + * Fetch. + * + * @param string $id Unique identifier of the entry. + * @param array $options Options array. + * + * @access public + * + * @return self Returns instance of The Arrays class. + */ + public function fetch(string $id, array $options = []): Arrays + { + return flextype('entries')->fetch($id, $options); + } +} + +class MediaTwig +{ + use Macroable; + + /** + * Create a Media Files instance. + */ + public function files(): MediaFilesTwig + { + return new MediaFilesTwig(); + } + + /** + * Create a Media Files instance. + */ + public function folders(): MediaFoldersTwig + { + return new MediaFoldersTwig(); + } +} + +class MediaFilesTwig +{ + use Macroable; + + /** + * Create a Media Files Meta instance. + */ + public function meta(): MediaFilesMetaTwig + { + return new MediaFilesMetaTwig(); + } + + /** + * Fetch. + * + * @param string $id The path to file. + * @param array $options Options array. + * + * @return self Returns instance of The Arrays class. + * + * @access public + */ + public function fetch(string $id, array $options = []): Arrays + { + return flextype('media')->files()->fetch($id, $options); + } + + /** + * Check whether a file exists. + * + * @param string $id Unique identifier of the file. + * + * @return bool True on success, false on failure. + * + * @access public + */ + public function has(string $id): bool + { + return flextype('media')->files()->has($id); + } + + /** + * Get file location + * + * @param string $id Unique identifier of the file. + * + * @return string entry file location + * + * @access public + */ + public function getFileLocation(string $id): bool + { + return flextype('media')->files()->getFileLocation($id); + } +} + +class MediaFilesMetaTwig +{ + use Macroable; + + /** + * Get file meta location + * + * @param string $id Unique identifier of the file. + * + * @return string entry file location + * + * @access public + */ + public function getFileMetaLocation(string $id): bool + { + return flextype('media')->files()->meta()->getFileMetaLocation($id); + } +} + +class MediaFoldersTwig +{ + use Macroable; + + /** + * Create a Media Folders Meta instance. + */ + public function meta(): MediaFoldersMetaTwig + { + return new MediaFoldersMetaTwig(); + } + + /** + * Fetch. + * + * @param string $id The path to folder. + * @param array $options Options array. + * + * @return self Returns instance of The Arrays class. + * + * @access public + */ + public function fetch(string $id, array $options = []): Arrays + { + return flextype('media')->folders()->fetch($id, $options); + } + + /** + * Get files directory location + * + * @param string $id Unique identifier of the folder. + * + * @return string entry directory location + * + * @access public + */ + public function getDirectoryLocation(string $id): string + { + return flextype('media')->folders()->getDirectoryLocation($id); + } +} + +class MediaFoldersMetaTwig +{ + use Macroable; + + /** + * Get files directory meta location + * + * @param string $id Unique identifier of the folder. + * + * @return string entry directory location + * + * @access public + */ + public function getDirectoryMetaLocation(string $id): string + { + return flextype('media')->folders()->meta()->getDirectoryMetaLocation($id); + } +} diff --git a/twig/JsonTwigExtension.php b/twig/JsonTwigExtension.php deleted file mode 100644 index f56e4e9..0000000 --- a/twig/JsonTwigExtension.php +++ /dev/null @@ -1,65 +0,0 @@ -json()->encode($input); - } - - /** - * Decode JSON - */ - public function decode(string $input, bool $cache = true) - { - return flextype('serializers')->json()->decode($input, $cache); - } -} diff --git a/twig/MarkdownTwigExtension.php b/twig/MarkdownTwigExtension.php deleted file mode 100644 index 5b85d0a..0000000 --- a/twig/MarkdownTwigExtension.php +++ /dev/null @@ -1,47 +0,0 @@ -markdown()->parse($input, $cache); - } - - return ''; - } -} diff --git a/twig/MediaTwigExtension.php b/twig/MediaTwigExtension.php deleted file mode 100644 index 4c5a601..0000000 --- a/twig/MediaTwigExtension.php +++ /dev/null @@ -1,110 +0,0 @@ - new MediaTwig(), - ]; - } -} - -class MediaTwig -{ - /** - * Create a Media Files instance. - */ - public function files(): MediaFiles - { - return new MediaTwigFiles(); - } - - /** - * Create a Media Files instance. - */ - public function folders(): MediaFolders - { - return new MediaTwigFolders(); - } -} - -class MediaTwigFiles -{ - public function meta(): MediaTwigFilesMeta - { - return new MediaTwigFilesMeta(); - } - - public function fetch(string $path, array $options = []): Arrays - { - return flextype('media')->files()->fetch($path, $options); - } - - public function has(string $path): bool - { - return flextype('media')->files()->has($path); - } - - public function getFileLocation(string $path): bool - { - return flextype('media')->files()->getFileLocation($path); - } -} - - -class MediaTwigFilesMeta -{ - public function getFileMetaLocation(string $path): bool - { - return flextype('media')->files()->meta()->getFileMetaLocation($path); - } -} - -class MediaTwigFolders -{ - public function meta(): MediaTwigFoldersMeta - { - return new MediaTwigFoldersMeta(); - } - - public function fetch(string $path, array $options = []): Arrays - { - return flextype('media')->folders()->fetch($path, $options); - } - - public function has(string $path): bool - { - return flextype('media')->folders()->has($path); - } - - public function getDirectoryLocation(string $path): bool - { - return flextype('media')->folders()->getDirectoryLocation($path); - } -} - - -class MediaTwigFoldersMeta -{ - public function getDirectoryMetaLocation(string $path): bool - { - return flextype('media')->folders()->meta()->getDirectoryMetaLocation($path); - } -} diff --git a/twig/RegistryTwigExtension.php b/twig/RegistryTwigExtension.php deleted file mode 100644 index f17ccb3..0000000 --- a/twig/RegistryTwigExtension.php +++ /dev/null @@ -1,96 +0,0 @@ - new RegistryTwig(), - ]; - } -} - -class RegistryTwig -{ - /** - * Flextype Application - */ - - - /** - * Constructor - */ - public function __construct() - { - - } - - /** - * Get registry array - * - * @return array - */ - public function dump() : array - { - return flextype('registry')->dump(); - } - - /** - * Checks if an object with this name is in the registry. - * - * @param string $name The name of the registry item to check for existence. - * @return bool - */ - public function has(string $name) : bool - { - return flextype('registry')->has($name); - } - - /** - * Registers a given value under a given name. - * - * @param string $name The name of the value to store. - * @param mixed $value The value that needs to be stored. - * @return void - */ - public function set(string $name, $value = null) : void - { - flextype('registry')->set($name, $value); - } - - /** - * Get item from the registry. - * - * @param string $name The name of the item to fetch. - * @param mixed $default Default value - * @return mixed - */ - public function get(string $name, $default = null) - { - return flextype('registry')->get($name, $default); - } -} diff --git a/twig/StringsTwigExtension.php b/twig/StringsTwigExtension.php index b3d191a..64b12c2 100644 --- a/twig/StringsTwigExtension.php +++ b/twig/StringsTwigExtension.php @@ -14,14 +14,6 @@ class StringsTwigExtension extends AbstractExtension { - /** - * Constructor - */ - public function __construct() - { - - } - /** * Callback for twig. * @@ -30,23 +22,7 @@ public function __construct() public function getFunctions() : array { return [ - new \Twig\TwigFunction('strings', [$this, 'strings']), + new \Twig\TwigFunction('strings', 'strings'), ]; } - - /** - * Create a new stringable object from the given string. - * - * Initializes a Strings object and assigns both $string and $encoding properties - * the supplied values. $string is cast to a string prior to assignment. Throws - * an InvalidArgumentException if the first argument is an array or object - * without a __toString method. - * - * @param mixed $string Value to modify, after being cast to string. Default: '' - * @param mixed $encoding The character encoding. Default: UTF-8 - */ - function strings($string = '', $encoding = 'UTF-8'): Strings - { - return new Strings($string, $encoding); - } } diff --git a/twig/UrlTwigExtension.php b/twig/UrlTwigExtension.php index d8295e1..cb16bd4 100644 --- a/twig/UrlTwigExtension.php +++ b/twig/UrlTwigExtension.php @@ -15,14 +15,6 @@ class UrlTwigExtension extends AbstractExtension { - /** - * Constructor - */ - public function __construct() - { - - } - /** * Callback for twig. * diff --git a/twig/YamlTwigExtension.php b/twig/YamlTwigExtension.php deleted file mode 100644 index 389d9c1..0000000 --- a/twig/YamlTwigExtension.php +++ /dev/null @@ -1,65 +0,0 @@ -yaml()->encode($input); - } - - /** - * Decode YAML - */ - public function decode(string $input, bool $cache = true) - { - return flextype('serializers')->yaml()->decode($input, $cache); - } -}