Updated Sasae for PHP 8.3 and Index changes.

This commit is contained in:
flash 2024-08-04 00:47:16 +00:00
parent c8a9f2974e
commit 54e8d0c877
12 changed files with 538 additions and 444 deletions

View file

@ -4,17 +4,15 @@
"type": "library", "type": "library",
"homepage": "https://railgun.sh/sasae", "homepage": "https://railgun.sh/sasae",
"license": "bsd-3-clause-clear", "license": "bsd-3-clause-clear",
"minimum-stability": "dev",
"prefer-stable": true,
"require": { "require": {
"php": ">=8.2", "php": ">=8.3",
"twig/twig": "^3.7", "twig/twig": "^3.10",
"twig/html-extra": "^3.7", "twig/html-extra": "^3.10",
"flashwave/index": "dev-master" "flashwave/index": "^0.2408.40014"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^10.2", "phpunit/phpunit": "^11.2",
"phpstan/phpstan": "^1.10" "phpstan/phpstan": "^1.11"
}, },
"authors": [ "authors": [
{ {

782
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,11 @@
<?php <?php
// SasaeFilesystemCache.php
// Created: 2024-08-04
// Updated: 2024-08-04
namespace Sasae\Cache; namespace Sasae\Cache;
use Twig\Cache\FilesystemCache as TwigFilesystemCache; use Twig\Cache\FilesystemCache as TwigFilesystemCache;
/** /**
* Extends Twig's filesystem cache implementation with an alternate constructor. * Extends Twig's filesystem cache implementation with an alternate constructor.
*/ */

View file

@ -1,11 +1,14 @@
<?php <?php
// SasaeExtension.php
// Created: 2024-08-04
// Updated: 2024-08-04
namespace Sasae\Extension; namespace Sasae\Extension;
use Index\ByteFormat; use Index\{ByteFormat,Index};
use Index\Environment as NdxEnvironment; use Sasae\{Sasae,SasaeEnvironment};
use Sasae\SasaeEnvironment; use Twig\{TwigFilter,TwigFunction};
use Twig\TwigFilter; use Twig\Environment as TwigEnvironment;
use Twig\TwigFunction;
use Twig\Extension\AbstractExtension as TwigAbstractExtension; use Twig\Extension\AbstractExtension as TwigAbstractExtension;
/** /**
@ -20,9 +23,9 @@ class SasaeExtension extends TwigAbstractExtension {
public function getFunctions() { public function getFunctions() {
return [ return [
new TwigFunction('ndx_version', NdxEnvironment::getIndexVersion(...)), new TwigFunction('ndx_version', Index::version(...)),
new TwigFunction('sasae_version', SasaeEnvironment::getSasaeVersion(...)), new TwigFunction('sasae_version', Sasae::version(...)),
new TwigFunction('twig_version', SasaeEnvironment::getTwigVersion(...)), new TwigFunction('twig_version', fn() => TwigEnvironment::VERSION),
]; ];
} }
} }

View file

@ -1,4 +1,8 @@
<?php <?php
// SasaeFilesystemLoader.php
// Created: 2024-08-04
// Updated: 2024-08-04
namespace Sasae\Loader; namespace Sasae\Loader;
use InvalidArgumentException; use InvalidArgumentException;

31
src/Sasae.php Normal file
View file

@ -0,0 +1,31 @@
<?php
// Sasae.php
// Created: 2024-08-04
// Updated: 2024-08-04
namespace Sasae;
final class Sasae {
public const PATH_SOURCE = __DIR__;
public const PATH_ROOT = self::PATH_SOURCE . DIRECTORY_SEPARATOR . '..';
public const PATH_VERSION = self::PATH_ROOT . DIRECTORY_SEPARATOR . 'VERSION';
/**
* Gets the current version of the Sasae library.
*
* Reads the VERSION file in the root of the Sasae directory.
* Returns 0.0.0 if reading the file failed for any reason.
*
* @return string Current version string.
*/
public static function version(): string {
if(!is_file(self::PATH_VERSION))
return '0.0.0';
$version = file_get_contents(self::PATH_VERSION);
if($version === false)
return '0.0.0';
return trim($version);
}
}

View file

@ -1,4 +1,8 @@
<?php <?php
// SasaeContext.php
// Created: 2024-08-04
// Updated: 2024-08-04
namespace Sasae; namespace Sasae;
use InvalidArgumentException; use InvalidArgumentException;

View file

@ -1,20 +1,17 @@
<?php <?php
// SasaeEnvironment.php // SasaeEnvironment.php
// Created: 2023-08-24 // Created: 2023-08-24
// Updated: 2023-08-24 // Updated: 2024-08-04
namespace Sasae; namespace Sasae;
use InvalidArgumentException; use InvalidArgumentException;
use UnexpectedValueException; use UnexpectedValueException;
use Index\Version;
use Sasae\Cache\SasaeFilesystemCache; use Sasae\Cache\SasaeFilesystemCache;
use Sasae\Extension\SasaeExtension; use Sasae\Extension\SasaeExtension;
use Sasae\Loader\SasaeFilesystemLoader; use Sasae\Loader\SasaeFilesystemLoader;
use Twig\{TwigFilter,TwigFunction,TwigTest};
use Twig\Environment as TwigEnvironment; use Twig\Environment as TwigEnvironment;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
use Twig\Cache\CacheInterface as TwigCacheInterface; use Twig\Cache\CacheInterface as TwigCacheInterface;
use Twig\Extension\ExtensionInterface as TwigExtensionInterface; use Twig\Extension\ExtensionInterface as TwigExtensionInterface;
use Twig\Extra\Html\HtmlExtension as TwigHtmlExtension; use Twig\Extra\Html\HtmlExtension as TwigHtmlExtension;
@ -26,10 +23,6 @@ use Twig\Loader\LoaderInterface as TwigLoaderInterface;
class SasaeEnvironment { class SasaeEnvironment {
private TwigEnvironment $env; private TwigEnvironment $env;
private static ?string $sasaeVersionString = null;
private static ?Version $sasaeVersion = null;
private static ?Version $twigVersion = null;
/** /**
* @param TwigLoaderInterface|string $loader A template loader instance or a path. * @param TwigLoaderInterface|string $loader A template loader instance or a path.
* @param TwigCacheInterface|array<string>|string|null $cache A caching driver. * @param TwigCacheInterface|array<string>|string|null $cache A caching driver.
@ -155,50 +148,4 @@ class SasaeEnvironment {
public function render(string $name, array $vars = []): string { public function render(string $name, array $vars = []): string {
return $this->env->render($name, $vars); return $this->env->render($name, $vars);
} }
/**
* Returns the current version of the Sasae library.
*
* @return Version
*/
public static function getSasaeVersion(): Version {
if(self::$sasaeVersion === null)
self::$sasaeVersion = Version::parse(self::getSasaeVersionString());
return self::$sasaeVersion;
}
/**
* Returns the current version of the Sasae library as a string.
*
* @return string
*/
public static function getSasaeVersionString(): string {
if(self::$sasaeVersionString === null) {
$body = file_get_contents(__DIR__ . '/../VERSION');
if($body === false)
throw new UnexpectedValueException('Was unable to read VERSION file.');
self::$sasaeVersionString = trim($body);
}
return self::$sasaeVersionString;
}
/**
* Returns the current version of the Twig library.
*
* @return Version
*/
public static function getTwigVersion(): Version {
if(self::$twigVersion === null)
self::$twigVersion = Version::parse(TwigEnvironment::VERSION);
return self::$twigVersion;
}
/**
* Returns the current version of the Twig library as a string.
*
* @return string
*/
public static function getTwigVersionString(): string {
return TwigEnvironment::VERSION;
}
} }

View file

@ -1,22 +1,25 @@
<?php <?php
// SasaeTest.php
// Created: 2024-08-04
// Updated: 2024-08-04
declare(strict_types=1); declare(strict_types=1);
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Index\Environment; use PHPUnit\Framework\Attributes\CoversClass;
use Index\XString; use Index\{Index,XString};
use Sasae\SasaeContext; use Sasae\{Sasae,SasaeContext,SasaeEnvironment};
use Sasae\SasaeEnvironment;
use Sasae\Cache\SasaeFilesystemCache; use Sasae\Cache\SasaeFilesystemCache;
use Sasae\Extension\SasaeExtension; use Sasae\Extension\SasaeExtension;
use Sasae\Loader\SasaeFilesystemLoader; use Sasae\Loader\SasaeFilesystemLoader;
use Twig\Environment as TwigEnvironment;
/** #[CoversClass(Sasae::class)]
* @covers SasaeContext #[CoversClass(SasaeContext::class)]
* @covers SasaeEnvironment #[CoversClass(SasaeEnvironment::class)]
* @covers SasaeExtension #[CoversClass(SasaeFilesystemCache::class)]
* @covers SasaeFilesystemCache #[CoversClass(SasaeExtension::class)]
* @covers SasaeFilesystemLoader #[CoversClass(SasaeFilesystemLoader::class)]
*/
final class SasaeTest extends TestCase { final class SasaeTest extends TestCase {
public function testEverything(): void { public function testEverything(): void {
$env = new SasaeEnvironment( $env = new SasaeEnvironment(
@ -28,9 +31,9 @@ final class SasaeTest extends TestCase {
$env->addGlobal('global_var', 'Sasae global var'); $env->addGlobal('global_var', 'Sasae global var');
$env->addGlobal('expect', [ $env->addGlobal('expect', [
'ndx_version' => (string)Environment::getIndexVersion(), 'ndx_version' => Index::version(),
'sasae_version' => SasaeEnvironment::getSasaeVersionString(), 'sasae_version' => Sasae::version(),
'twig_version' => SasaeEnvironment::getTwigVersionString(), 'twig_version' => TwigEnvironment::VERSION,
]); ]);
$env->addFilter('test_filter', fn($text) => ('filter:' . $text)); $env->addFilter('test_filter', fn($text) => ('filter:' . $text));

30
tools/create-tag Executable file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env php
<?php
$path = (function($path) {
if(!str_starts_with($path, '/'))
die('Cannot be bothered to support non-UNIX style paths, sorry!' . PHP_EOL);
while($path !== '/') {
$vPath = $path . DIRECTORY_SEPARATOR . 'VERSION';
if(is_file($vPath))
return $vPath;
$path = dirname($path);
}
})(__DIR__);
$version = file_get_contents($path);
if($version === false)
die('Failed to read VERSION file.' . PHP_EOL);
$version = trim($version);
$workingDir = getcwd();
try {
chdir(dirname($path));
echo shell_exec(sprintf('git tag v%s', $version));
echo shell_exec(sprintf('git push origin v%s', $version));
} finally {
chdir($workingDir);
}
echo $version . PHP_EOL;

View file

@ -1,8 +1,8 @@
#!/bin/bash #!/usr/bin/env bash
pushd . pushd .
cd $(dirname "$0") cd $(dirname "$0")
php update-headers.php ./update-headers
popd popd

1
tools/update-headers.php → tools/update-headers Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php <?php
// the point of index was so that i wouldn't have to copy things between projects // the point of index was so that i wouldn't have to copy things between projects
// here i am copying things from index // here i am copying things from index