Skip to content

Commit

Permalink
284 Render helper ensuredir function bug (#292)
Browse files Browse the repository at this point in the history
Fix #284 

Previously proposed by @arun744-newscorp, but never brought to the end.
I've just added the needed type declarations to the brought-back
original #283

:)
  • Loading branch information
alessandro-fazzi committed Apr 14, 2023
2 parents 68f3170 + ba239b1 commit 5861e28
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
61 changes: 61 additions & 0 deletions rtfm/source/_pages/usage/filters.rst
Expand Up @@ -63,6 +63,67 @@ The default path is ``blocks/``.

The path will be always relative to ``views/`` folder

wordless_tmp_dir_exists
##########################

.. literalinclude:: /../../wordless/helpers/render_helper.php
:emphasize-lines: 255
:language: php
:caption: wordless/helpers/render_helper.php

**Usage example**

.. code-block:: php
<?php
function validateWordlessTmpDir( $tmpdir, $coreCheckResult ){
if (false == $coreCheckResult) { // Just giving examples
sendAlertToSysadmin();
}
$file_counts = 0;
if ( file_exists( $tmpdir ) ) {
$file_counts = preg_grep('/(.*).(php|txt)$/', scandir( $tmpdir ) );
}
return count( $file_counts ) > 0;
}
add_filter('wordless_tmp_dir_exists', 'validateWordlessTmpDir', 10, 2);
Sometimes tmp folder in theme directory, may not have write permission in dedicated server, Hence failure to load pug template from tmp.
In tmp directory, if there is compiled files listed following hook can be used to check file counts and override ensure_tmp_dir function to return true.
In some cases files can be compiled via command line to generate files in tmp dir.
here the filter code is added in ``themes/exmaple-theme/config/initializers/hooks.php``

wordless_environment
##########################

.. literalinclude:: /../../wordless/helpers/render_helper.php
:emphasize-lines: 110
:language: php
:caption: wordless/helpers/render_helper.php

**Usage example**

.. code-block:: php
<?php
add_filter( 'wordless_environment' , function( $pug_environment ) {
$env = getCustomEnv(); // example a custom define global function
if ( $env->is_local() ) {
return $pug_environment;
}
return 'production';
} );
Here the filter code is added in ``themes/exmaple-theme/config/initializers/hooks.php``

If there is different or custom environment name defined this hook can override it, rather than defaulting to development always. For example if environment is called UAT or SIT.

Actions
=======

Expand Down
21 changes: 16 additions & 5 deletions wordless/helpers/render_helper.php
Expand Up @@ -88,7 +88,7 @@ function render_template($name, $locals = array(), $static = false) {
case 'pug':
require_once('pug/wordless_pug_options.php');

if ($this->ensure_dir($tmp_dir)) {
if ($this->ensure_tmp_dir() ) {
// Read the environment from various sources. Note that .env file has precedence
if ( getenv('ENVIRONMENT') ) {
$env = getenv('ENVIRONMENT');
Expand All @@ -107,6 +107,8 @@ function render_template($name, $locals = array(), $static = false) {
$bypass_static = 'false'; // default value
}

$env = apply_filters( 'wordless_environment', $env );

if ( in_array( $env, array('staging', 'production') ) ) {
if (true === $static && 'false' == strtolower($bypass_static)) {
$staticPath = $this->static_path($name, $locals);
Expand Down Expand Up @@ -247,7 +249,16 @@ function wl_yield() {
render_template($current_view, $current_locals);
}

private function ensure_dir($dir) {
private function ensure_tmp_dir() : bool {
$tmpDir = Wordless::theme_temp_path();
$tmp_dir_exists_and_writable = $this->ensure_dir( $tmpDir );

return apply_filters('wordless_tmp_dir_exists', $tmpDir, $tmp_dir_exists_and_writable );
}

private function ensure_dir( $dir ) : bool {

$dir_exists_and_writable = false;

if (!file_exists($dir)) {
mkdir($dir, 0770);
Expand All @@ -258,10 +269,10 @@ private function ensure_dir($dir) {
}

if (is_writable($dir)) {
return true;
} else {
return false;
$dir_exists_and_writable = true;
}

return $dir_exists_and_writable;
}

// REALLY IMPORTANT NOTE: the cache policy of static generated views is based on the
Expand Down

0 comments on commit 5861e28

Please sign in to comment.