diff --git a/.github/workflows/coding-style.yml b/.github/workflows/coding-style.yml index f383b46a..5b796f6c 100644 --- a/.github/workflows/coding-style.yml +++ b/.github/workflows/coding-style.yml @@ -10,7 +10,7 @@ jobs: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none - run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress @@ -24,7 +24,7 @@ jobs: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none - run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 5a39324e..8af71c68 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none - run: composer install --no-progress --prefer-dist diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ee42cae3..a7245702 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['8.0', '8.1', '8.2', '8.3', '8.4'] + php: ['8.1', '8.2', '8.3', '8.4'] fail-fast: false @@ -37,7 +37,7 @@ jobs: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none - run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable @@ -52,7 +52,7 @@ jobs: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none - run: composer install --no-progress --prefer-dist diff --git a/composer.json b/composer.json index d4782dac..6acc7c7e 100644 --- a/composer.json +++ b/composer.json @@ -15,19 +15,19 @@ } ], "require": { - "php": "8.0 - 8.4", + "php": "8.1 - 8.4", "nette/utils": "^4.0" }, "require-dev": { "nette/tester": "^2.4", "nette/di": "^3.1 || ^4.0", - "latte/latte": "^2.11 || ^3.0.12", + "latte/latte": "^3.0.12", "tracy/tracy": "^2.9", "phpstan/phpstan": "^1.0", "psr/simple-cache": "^2.0 || ^3.0" }, "conflict": { - "latte/latte": ">=3.0.0 <3.0.12" + "latte/latte": "<3.0.12" }, "suggest": { "ext-pdo_sqlite": "to use SQLiteStorage or SQLiteJournal" @@ -42,7 +42,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.3-dev" + "dev-master": "4.0-dev" } } } diff --git a/readme.md b/readme.md index 77a0953e..bb2ccd08 100644 --- a/readme.md +++ b/readme.md @@ -33,7 +33,7 @@ Installation composer require nette/caching ``` -It requires PHP version 8.0 and supports PHP up to 8.4. +It requires PHP version 8.1 and supports PHP up to 8.4. Basic Usage diff --git a/src/Bridges/CacheDI/CacheExtension.php b/src/Bridges/CacheDI/CacheExtension.php index da6b1f89..5d1ff9d2 100644 --- a/src/Bridges/CacheDI/CacheExtension.php +++ b/src/Bridges/CacheDI/CacheExtension.php @@ -45,13 +45,5 @@ public function loadConfiguration(): void $builder->addDefinition($this->prefix('storage')) ->setType(Nette\Caching\Storage::class) ->setFactory(Nette\Caching\Storages\FileStorage::class, [$this->tempDir]); - - if ($this->name === 'cache') { - if (extension_loaded('pdo_sqlite')) { - $builder->addAlias('nette.cacheJournal', $this->prefix('journal')); - } - - $builder->addAlias('cacheStorage', $this->prefix('storage')); - } } } diff --git a/src/Bridges/CacheLatte/CacheMacro.php b/src/Bridges/CacheLatte/CacheMacro.php deleted file mode 100644 index d6aeb5db..00000000 --- a/src/Bridges/CacheLatte/CacheMacro.php +++ /dev/null @@ -1,168 +0,0 @@ -used = false; - } - - - /** - * Finishes template parsing. - * @return array(prolog, epilog) - */ - public function finalize() - { - if ($this->used) { - return ['Nette\Bridges\CacheLatte\CacheMacro::initRuntime($this);']; - } - } - - - /** - * New node is found. - * @return bool - */ - public function nodeOpened(Latte\MacroNode $node) - { - if ($node->modifiers) { - throw new Latte\CompileException('Modifiers are not allowed in ' . $node->getNotation()); - } - - $this->used = true; - $node->empty = false; - $node->openingCode = Latte\PhpWriter::using($node) - ->write( - 'global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) /* line %var */ try { ?>', - Nette\Utils\Random::generate(), - $node->startLine, - ); - } - - - /** - * Node is closed. - * @return void - */ - public function nodeClosed(Latte\MacroNode $node) - { - $node->closingCode = Latte\PhpWriter::using($node) - ->write( - 'global->cacheStack, %node.array?) /* line %var */; - } catch (\Throwable $ʟ_e) { - Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); throw $ʟ_e; - } ?>', - $node->startLine, - ); - } - - - /********************* run-time helpers ****************d*g**/ - - - public static function initRuntime(Latte\Runtime\Template $template): void - { - if (!empty($template->global->cacheStack)) { - $file = (new \ReflectionClass($template))->getFileName(); - if (@is_file($file)) { // @ - may trigger error - end($template->global->cacheStack)->dependencies[Cache::Files][] = $file; - } - } - } - - - /** - * Starts the output cache. Returns Nette\Caching\OutputHelper object if buffering was started. - */ - public static function createCache( - Nette\Caching\Storage $cacheStorage, - string $key, - ?array &$parents, - ?array $args = null, - ): Nette\Caching\OutputHelper|\stdClass|null - { - if ($args) { - if (array_key_exists('if', $args) && !$args['if']) { - return $parents[] = new \stdClass; - } - - $key = array_merge([$key], array_intersect_key($args, range(0, count($args)))); - } - - if ($parents) { - end($parents)->dependencies[Cache::Items][] = $key; - } - - $cache = new Cache($cacheStorage, 'Nette.Templating.Cache'); - if ($helper = $cache->capture($key)) { - $parents[] = $helper; - } - - return $helper; - } - - - /** - * Ends the output cache. - * @param Nette\Caching\OutputHelper[] $parents - */ - public static function endCache(array &$parents, ?array $args = null): void - { - $helper = array_pop($parents); - if (!$helper instanceof Nette\Caching\OutputHelper) { - return; - } - - if (isset($args['dependencies'])) { - $args += $args['dependencies'](); - } - - if (isset($args['expire'])) { - $args['expiration'] = $args['expire']; // back compatibility - } - - $helper->dependencies[Cache::Tags] = $args['tags'] ?? null; - $helper->dependencies[Cache::Expire] = $args['expiration'] ?? '+ 7 days'; - $helper->end(); - } - - - /** - * @param Nette\Caching\OutputHelper[] $parents - */ - public static function rollback(array &$parents): void - { - $helper = array_pop($parents); - if ($helper instanceof Nette\Caching\OutputHelper) { - $helper->rollback(); - } - } -} diff --git a/src/Caching/BulkWriter.php b/src/Caching/BulkWriter.php index 38d88fb4..7c50fb52 100644 --- a/src/Caching/BulkWriter.php +++ b/src/Caching/BulkWriter.php @@ -17,7 +17,7 @@ interface BulkWriter { /** * Writes to cache in bulk. - * @param array{string, mixed} $items + * @param array $items */ function bulkWrite(array $items, array $dependencies): void; diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php index 4f4df05b..c458afc9 100644 --- a/src/Caching/Cache.php +++ b/src/Caching/Cache.php @@ -64,9 +64,7 @@ class Cache public const ALL = self::All; /** @internal */ - public const - NamespaceSeparator = "\x00", - NAMESPACE_SEPARATOR = self::NamespaceSeparator; + public const NamespaceSeparator = "\x00"; private Storage $storage; private string $namespace; @@ -385,6 +383,7 @@ public function capture(mixed $key): ?OutputHelper */ public function start($key): ?OutputHelper { + trigger_error(__METHOD__ . '() was renamed to capture()', E_USER_DEPRECATED); return $this->capture($key); } @@ -394,7 +393,7 @@ public function start($key): ?OutputHelper */ protected function generateKey($key): string { - return $this->namespace . md5(is_scalar($key) ? (string) $key : serialize($key)); + return $this->namespace . hash('xxh128', is_scalar($key) ? (string) $key : serialize($key)); } diff --git a/src/Caching/Storage.php b/src/Caching/Storage.php index e9565d8b..16b80942 100644 --- a/src/Caching/Storage.php +++ b/src/Caching/Storage.php @@ -17,9 +17,8 @@ interface Storage { /** * Read from cache. - * @return mixed */ - function read(string $key); + function read(string $key): mixed; /** * Prevents item reading and writing. Lock is released by write() or remove(). diff --git a/src/Caching/Storages/MemcachedStorage.php b/src/Caching/Storages/MemcachedStorage.php index 6fe9ce43..51767577 100644 --- a/src/Caching/Storages/MemcachedStorage.php +++ b/src/Caching/Storages/MemcachedStorage.php @@ -105,7 +105,7 @@ public function bulkRead(array $keys): array { $prefixedKeys = array_map(fn($key) => urlencode($this->prefix . $key), $keys); $keys = array_combine($prefixedKeys, $keys); - $metas = $this->memcached->getMulti($prefixedKeys); + $metas = $this->memcached->getMulti($prefixedKeys) ?: []; $result = []; $deleteKeys = []; foreach ($metas as $prefixedKey => $meta) { diff --git a/src/Caching/Storages/SQLiteJournal.php b/src/Caching/Storages/SQLiteJournal.php index 26747834..cf96ca3a 100644 --- a/src/Caching/Storages/SQLiteJournal.php +++ b/src/Caching/Storages/SQLiteJournal.php @@ -40,7 +40,6 @@ private function open(): void } $this->pdo = new \PDO('sqlite:' . $this->path); - $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->pdo->exec(' PRAGMA foreign_keys = OFF; PRAGMA journal_mode = WAL; diff --git a/src/Caching/Storages/SQLiteStorage.php b/src/Caching/Storages/SQLiteStorage.php index 5301a4e7..016a4aba 100644 --- a/src/Caching/Storages/SQLiteStorage.php +++ b/src/Caching/Storages/SQLiteStorage.php @@ -28,7 +28,6 @@ public function __construct(string $path) } $this->pdo = new \PDO('sqlite:' . $path); - $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->pdo->exec(' PRAGMA foreign_keys = ON; CREATE TABLE IF NOT EXISTS cache ( diff --git a/tests/Bridges.DI/CacheExtension.phpt b/tests/Bridges.DI/CacheExtension.phpt index cc735739..9f4c4703 100644 --- a/tests/Bridges.DI/CacheExtension.phpt +++ b/tests/Bridges.DI/CacheExtension.phpt @@ -28,8 +28,4 @@ test('', function () { $storage = $container->getService('cache.storage'); Assert::type(Nette\Caching\Storages\FileStorage::class, $storage); - - // aliases - Assert::same($journal, $container->getService('nette.cacheJournal')); - Assert::same($storage, $container->getService('cacheStorage')); }); diff --git a/tests/Bridges.Latte2/CacheMacro.cache.phpt b/tests/Bridges.Latte2/CacheMacro.cache.phpt deleted file mode 100644 index 8dfa099f..00000000 --- a/tests/Bridges.Latte2/CacheMacro.cache.phpt +++ /dev/null @@ -1,48 +0,0 @@ -')) { - Tester\Environment::skip('Test for Latte 2'); -} - - -$latte = new Latte\Engine; -$latte->setTempDirectory(getTempDir()); -$latte->addMacro('cache', new CacheMacro($latte->getCompiler())); -$latte->addProvider('cacheStorage', new Nette\Caching\Storages\MemoryStorage); - -$params['title'] = 'Hello'; -$params['id'] = 456; - -Assert::matchFile( - __DIR__ . '/expected/CacheMacro.cache.php', - $latte->compile(__DIR__ . '/templates/cache.latte'), -); -Assert::matchFile( - __DIR__ . '/expected/CacheMacro.cache.html', - $latte->renderToString( - __DIR__ . '/templates/cache.latte', - $params, - ), -); -Assert::matchFile( - __DIR__ . '/expected/CacheMacro.cache.html', - $latte->renderToString( - __DIR__ . '/templates/cache.latte', - $params, - ), -); -Assert::matchFile( - __DIR__ . '/expected/CacheMacro.cache.inc.php', - file_get_contents($latte->getCacheFile(__DIR__ . strtr('/templates/include.cache.latte', '/', DIRECTORY_SEPARATOR))), -); diff --git a/tests/Bridges.Latte2/CacheMacro.createCache.phpt b/tests/Bridges.Latte2/CacheMacro.createCache.phpt deleted file mode 100644 index 5cd26089..00000000 --- a/tests/Bridges.Latte2/CacheMacro.createCache.phpt +++ /dev/null @@ -1,49 +0,0 @@ -')) { - Tester\Environment::skip('Test for Latte 2'); -} - - -test('', function () { - $parents = []; - $dp = [Cache::Tags => ['rum', 'cola']]; - $outputHelper = CacheMacro::createCache(new DevNullStorage, 'test', $parents); - Assert::type(Nette\Caching\OutputHelper::class, $outputHelper); - CacheMacro::endCache($parents, $dp); - Assert::same($dp + [Cache::Expire => '+ 7 days'], $outputHelper->dependencies); -}); - -test('', function () { - $parents = []; - $dp = [Cache::Tags => ['rum', 'cola']]; - $dpFallback = fn() => $dp; - $outputHelper = CacheMacro::createCache(new DevNullStorage, 'test', $parents); - CacheMacro::endCache($parents, ['dependencies' => $dpFallback]); - Assert::same($dp + [Cache::Expire => '+ 7 days'], $outputHelper->dependencies); -}); - -test('', function () { - $parents = []; - $dp = [ - Cache::Tags => ['rum', 'cola'], - Cache::Expire => '+ 1 days', - ]; - $dpFallback = fn() => $dp; - $outputHelper = CacheMacro::createCache(new DevNullStorage, 'test', $parents); - CacheMacro::endCache($parents, ['dependencies' => $dpFallback]); - Assert::same($dp, $outputHelper->dependencies); -}); diff --git a/tests/Bridges.Latte2/expected/CacheMacro.cache.html b/tests/Bridges.Latte2/expected/CacheMacro.cache.html deleted file mode 100644 index 0fe8eebc..00000000 --- a/tests/Bridges.Latte2/expected/CacheMacro.cache.html +++ /dev/null @@ -1,8 +0,0 @@ -Noncached content - - -

HELLO

- -

Included file (11)

- - hello diff --git a/tests/Bridges.Latte2/expected/CacheMacro.cache.inc.php b/tests/Bridges.Latte2/expected/CacheMacro.cache.inc.php deleted file mode 100644 index af95b58b..00000000 --- a/tests/Bridges.Latte2/expected/CacheMacro.cache.inc.php +++ /dev/null @@ -1,12 +0,0 @@ -global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) /* line %d% */ try { - echo ' '; - echo LR\Filters::escapeHtmlText(($this->filters->lower)($title)) /* line %d% */; - echo "\n"; - Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack) /* line %d% */; - } catch (\Throwable $ʟ_e) { - Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); - throw $ʟ_e; - } -%A% diff --git a/tests/Bridges.Latte2/expected/CacheMacro.cache.php b/tests/Bridges.Latte2/expected/CacheMacro.cache.php deleted file mode 100644 index 491297ec..00000000 --- a/tests/Bridges.Latte2/expected/CacheMacro.cache.php +++ /dev/null @@ -1,28 +0,0 @@ -global->cacheStorage, '%[\w]+%', $this->global->cacheStack, [$id, 'tags' => 'mytag'])) /* line %d% */ try { - echo ' -

'; - echo LR\Filters::escapeHtmlText(($this->filters->upper)($title)) /* line %d% */; - echo '

- -'; - $this->createTemplate('include.cache.latte', ['localvar' => 11] + $this->params, 'include')->renderToContentType('html') /* line %d% */; - echo "\n"; - Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, [$id, 'tags' => 'mytag']) /* line %d% */; - } catch (\Throwable $ʟ_e) { - Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); - throw $ʟ_e; - } -%A% - } - - - public function prepare(): void - { -%A% - Nette\Bridges\CacheLatte\CacheMacro::initRuntime($this); -%A% diff --git a/tests/Bridges.Latte2/templates/cache.latte b/tests/Bridges.Latte2/templates/cache.latte deleted file mode 100644 index 5155ee77..00000000 --- a/tests/Bridges.Latte2/templates/cache.latte +++ /dev/null @@ -1,9 +0,0 @@ -Noncached content - -{cache $id, tags => 'mytag'} - -

{$title|upper}

- -{include 'include.cache.latte', 'localvar' => 11} - -{/cache} diff --git a/tests/Bridges.Latte2/templates/include.cache.latte b/tests/Bridges.Latte2/templates/include.cache.latte deleted file mode 100644 index 595329b6..00000000 --- a/tests/Bridges.Latte2/templates/include.cache.latte +++ /dev/null @@ -1,5 +0,0 @@ -

Included file ({$localvar})

- -{cache} - {$title|lower} -{/cache} diff --git a/tests/Bridges.Latte3/Runtime.phpt b/tests/Bridges.Latte3/Runtime.phpt index 83a5121b..2360125e 100644 --- a/tests/Bridges.Latte3/Runtime.phpt +++ b/tests/Bridges.Latte3/Runtime.phpt @@ -9,10 +9,6 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -if (version_compare(Latte\Engine::VERSION, '3', '<')) { - Tester\Environment::skip('Test for Latte 3'); -} - test('', function () { $runtime = new Runtime(new DevNullStorage); diff --git a/tests/Bridges.Latte3/{cache}.phpt b/tests/Bridges.Latte3/{cache}.phpt index e275b259..cb5ea876 100644 --- a/tests/Bridges.Latte3/{cache}.phpt +++ b/tests/Bridges.Latte3/{cache}.phpt @@ -11,10 +11,6 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -if (version_compare(Latte\Engine::VERSION, '3', '<')) { - Tester\Environment::skip('Test for Latte 3'); -} - $latte = new Latte\Engine; $latte->setTempDirectory(getTempDir()); diff --git a/tests/Storages/SQLiteJournal.permissions.phpt b/tests/Storages/SQLiteJournal.permissions.phpt index 396f6eb1..03f92c59 100644 --- a/tests/Storages/SQLiteJournal.permissions.phpt +++ b/tests/Storages/SQLiteJournal.permissions.phpt @@ -26,7 +26,7 @@ test('', function () { umask(0); (new SQLiteJournal($file))->write('foo', []); - Assert::same(0666, fileperms($file) & 0777); + Assert::same(0o666, fileperms($file) & 0o777); }); @@ -34,8 +34,8 @@ test('', function () { $file = getTempDir() . '/sqlitejournal.permissions.2.sqlite'; Assert::false(file_exists($file)); - umask(0077); + umask(0o077); (new SQLiteJournal($file))->write('foo', []); - Assert::same(0600, fileperms($file) & 0777); + Assert::same(0o600, fileperms($file) & 0o777); }); diff --git a/tests/Storages/SQLiteStorage.permissions.phpt b/tests/Storages/SQLiteStorage.permissions.phpt index 331b2861..153ac19a 100644 --- a/tests/Storages/SQLiteStorage.permissions.phpt +++ b/tests/Storages/SQLiteStorage.permissions.phpt @@ -26,7 +26,7 @@ test('', function () { umask(0); (new SQLiteStorage($file))->write('foo', 'bar', []); - Assert::same(0666, fileperms($file) & 0777); + Assert::same(0o666, fileperms($file) & 0o777); }); @@ -34,8 +34,8 @@ test('', function () { $file = getTempDir() . '/sqlitestorage.permissions.2.sqlite'; Assert::false(file_exists($file)); - umask(0077); + umask(0o077); (new SQLiteStorage($file))->write('foo', 'bar', []); - Assert::same(0600, fileperms($file) & 0777); + Assert::same(0o600, fileperms($file) & 0o777); });