Skip to content

Commit

Permalink
added in the ability to do cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-gareth committed May 16, 2024
1 parent a165b5f commit 1edc86b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Motiontactic;

use Illuminate\Support\Facades\File;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Flex
Expand Down Expand Up @@ -44,9 +45,38 @@ public function build_flexible_components()
}

public function build()
{
if ( function_exists( 'storage_path' ) && defined( 'FLEX_CACHE' ) && FLEX_CACHE ) $this->cache();
if ( function_exists( 'storage_path' ) && $this->exists( 'flex.php' ) ) {
$build = include $this->path( 'flex.php' );
} else {
$this->flex = apply_filters( 'mt_flexible_component_before_build', $this->flex );
$build = $this->flex->build();
}
\acf_add_local_field_group( $build );
}

private function path( $filename = null )
{
$directory = storage_path( 'framework/cache' );
if ( !$filename ) return $directory;
return $directory . '/' . $filename;
}

private function exists( $filename = null ): bool
{
return file_exists( $this->path( $filename ) );
}

private function cache()
{
$this->flex = apply_filters( 'mt_flexible_component_before_build', $this->flex );
\acf_add_local_field_group( $this->flex->build() );
$build = $this->flex->build();
File::ensureDirectoryExists( $this->path() );
return file_put_contents(
$this->path( 'flex.php' ),
'<?php return ' . var_export( $build, true ) . ';'
) !== false ? count( $build ) : null;
}

public function from_camel_case( $input )
Expand All @@ -67,4 +97,4 @@ public static function getInstance()

return self::$instance;
}
}
}

0 comments on commit 1edc86b

Please sign in to comment.