Skip to content

Commit

Permalink
Add caching service
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 8, 2024
1 parent e7bea13 commit 7a49ddb
Show file tree
Hide file tree
Showing 157 changed files with 15,475 additions and 81 deletions.
15 changes: 6 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"php": ">=7.4",
"ext-json": "*",
"erusev/parsedown": "^1.7",
"ramsey/uuid": "^4.2",
"salesforce/handlebars-php": "3.0.1"
"ramsey/uuid": "^4.2"
},
"config": {
"platform": {
Expand All @@ -47,18 +46,16 @@
"lint-report": "phpcs --standard=./.phpcs.xml.dist --report=checkstyle",
"fix": "php-cs-fixer fix .",
"prefix-dependencies": [
"@composer --working-dir=php-scoper install",
"cd php-scoper && vendor/bin/php-scoper --output-dir=../vendor_prefixed add-prefix --force && cd ..",
"@composer dump-autoload -o -a"
"composer --working-dir=php-scoper install",
"cd php-scoper && vendor/bin/php-scoper add-prefix --output-dir=../vendor_prefixed --force && cd ..",
"composer dump-autoload -o",
"php generate_autoload.php"
]
},
"autoload": {
"psr-4": {
"WCPOS\\WooCommercePOS\\": "includes/"
},
"classmap": [
"vendor_prefixed/vendor/firebase/php-jwt/src"
]
}
},
"autoload-dev": {
"psr-4": {
Expand Down
70 changes: 70 additions & 0 deletions generate_autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

$vendorDir = __DIR__ . '/vendor_prefixed';
$autoloadFile = $vendorDir . '/autoload.php';

$files = array();

$directory = new RecursiveDirectoryIterator( $vendorDir, RecursiveDirectoryIterator::SKIP_DOTS );
$iterator = new RecursiveIteratorIterator( $directory, RecursiveIteratorIterator::SELF_FIRST );

// Function to strip comments from PHP code
function strip_comments( $source ) {
// Regular expressions for removing single-line and multi-line comments
$singleLineCommentPattern = '/\/\/.*$/m';
$multiLineCommentPattern = '/\/\*.*?\*\//s';
// Remove the comments from the source code
$source = preg_replace( $singleLineCommentPattern, '', $source );
$source = preg_replace( $multiLineCommentPattern, '', $source );
return $source;
}

foreach ( $iterator as $file ) {
if ( $file->isFile() && $file->getExtension() === 'php' ) {
$relativePath = str_replace( $vendorDir . '/', '', $file->getPathname() );

// Read the namespace and class name from the file
$fileContent = file_get_contents( $file->getPathname() );

// Strip comments from the file content
$fileContent = strip_comments( $fileContent );

$namespace = '';
$class = '';

if ( preg_match( '/namespace\s+([^;\s]+)\s*;/', $fileContent, $matches ) ) {
$namespace = $matches[1];
}
if ( preg_match( '/\b(class|interface|trait)\s+([^\s{]+)/', $fileContent, $classMatches ) ) {
$class = $classMatches[2];
}

// Only add valid class mappings
if ( $namespace && $class ) {
$fullClassName = $namespace . '\\' . $class;
echo 'Found class: ' . $fullClassName . PHP_EOL;
$files[ $fullClassName ] = './' . $relativePath;
}
}
}

echo 'Number of entries in files array: ' . count( $files ) . PHP_EOL;

$autoloadContent = "<?php\n\n";
$autoloadContent .= "// autoload.php @generated by Composer\n\n";
$autoloadContent .= "\$classMap = [\n";

foreach ( $files as $className => $path ) {
$autoloadContent .= " '$className' => '$path',\n";
}

$autoloadContent .= "];\n\n";
$autoloadContent .= "spl_autoload_register(function (\$class) use (\$classMap) {\n";
$autoloadContent .= " if (isset(\$classMap[\$class])) {\n";
$autoloadContent .= " require __DIR__ . '/' . \$classMap[\$class];\n";
$autoloadContent .= " }\n";
$autoloadContent .= "});\n";

file_put_contents( $autoloadFile, $autoloadContent );

echo "Autoload file generated at $autoloadFile\n";
52 changes: 52 additions & 0 deletions includes/Services/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Cache Helper class
*/

namespace WCPOS\WooCommercePOS\Services;

use WCPOS\Vendor\Phpfastcache\Drivers\Files\Config;
use WCPOS\Vendor\Phpfastcache\Drivers\Files\Driver;
use WCPOS\Vendor\Phpfastcache\EventManager;
use WCPOS\Vendor\Phpfastcache\Helper\Psr16Adapter;

/**
*
*/
class Cache {
/**
* Get the cache instance
*
* @param string $instance_id Instance ID.
*
* @return Psr16Adapter
*/
public static function get_cache_instance( string $instance_id = 'default' ) {
static $cache = null;

if ( $cache === null ) {
$upload_dir = wp_upload_dir();
$cache_dir = $upload_dir['basedir'] . '/woocommerce_pos_cache';

if ( ! file_exists( $cache_dir ) ) {
wp_mkdir_p( $cache_dir );
}

$config = new Config(
array(
'path' => $cache_dir,
)
);

$event_manager = EventManager::getInstance();

// Generate a unique instance ID for each logged-in user.
$driver = new Driver( $config, $instance_id );
$driver->setEventManager( $event_manager );

$cache = new Psr16Adapter( $driver );
}

return $cache;
}
}
4 changes: 2 additions & 2 deletions php-scoper/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"require": {
"php": ">=7.4",
"ext-json": "*",
"firebase/php-jwt": "v6.9.0"
"firebase/php-jwt": "v6.9.0",
"phpfastcache/phpfastcache": "^8.1"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -17,4 +18,3 @@
"sort-packages": true
}
}

2 changes: 0 additions & 2 deletions php-scoper/dummy/dummy.php

This file was deleted.

50 changes: 9 additions & 41 deletions php-scoper/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,18 @@
use Isolated\Symfony\Component\Finder\Finder;

return array(
// Define the namespace prefix to use.
'prefix' => 'WCPOS\Vendor',
'prefix' => 'WCPOS\\Vendor',

// Finders: Locate files that need to be scoped.
'finders' => array(
// Finder for the firebase/php-jwt library.
Finder::create()->files()
->in( 'vendor/firebase/php-jwt' )
->name( '*.php' ), // Scope only PHP files.

/**
* If there is only one library the folder structure is not maintained :(
* By creating a dummy folder we can maintain the folder structure.
*/
Finder::create()->files()
->in( 'dummy' )
->name( '*.php' ),

// Add Finder for yahnis-elsts/plugin-update-checker
// NOTE: I ended up rolling my own update checker, so this is no longer needed.
// Finder::create()->files()
// ->in( 'vendor/yahnis-elsts/plugin-update-checker' )
// ->name( '*.php' ), // Scope only PHP files.

// Add Finder for ramsey/uuid
// NOTE: UUID has too many dependencies to scope, so we'll just leave it alone.
// Finder::create()->files()
// ->in( 'vendor/ramsey/uuid' )
// ->name( '*.php' ), // Scope only PHP files.
Finder::create()->files()->in( 'vendor/firebase/php-jwt' )->name( '*.php' ),
Finder::create()->files()->in( 'vendor/phpfastcache/phpfastcache' )->name( '*.php' ),
),

// 'patchers' are used to transform the code after it has been scoped.
// Define any necessary patchers below. For a minimal setup, this might not be needed.
'patchers' => array(
// Example patcher (you can modify or remove this)
function ( string $filePath, string $prefix, string $content ) {
// Modify $content as needed or return it unchanged.
return $content;
},
),

// 'whitelist' can be used to specify classes, functions, and constants
// that should not be prefixed (i.e., left in the global scope).
'whitelist' => array(
// Example: 'YourNamespacePrefix\Firebase\JWT\*',
'patchers' => array(
function ( string $filePath, string $prefix, string $content ) {
return $content;
},
),

'whitelist' => array(),
);
Loading

0 comments on commit 7a49ddb

Please sign in to comment.