Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Mar 20, 2023
0 parents commit e32fec4
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
coverage:
status:
project:
default:
target: auto
threshold: 1%
patch:
default:
target: auto
threshold: 50%

comment: false
github_checks:
annotations: false
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_size = 2
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.github export-ignore
/tests export-ignore
phpunit.dist.xml export-ignore
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI

on:
push:
pull_request:
schedule:
- cron: '0 0 1,16 * *'

jobs:
tests:
uses: zenstruck/.github/.github/workflows/php-test.yml@main

code-coverage:
uses: zenstruck/.github/.github/workflows/php-coverage-codecov.yml@main

composer-validate:
uses: zenstruck/.github/.github/workflows/php-composer-validate.yml@main

sca:
uses: zenstruck/.github/.github/workflows/php-stan.yml@main

fixcs:
name: Run php-cs-fixer
needs: sync-with-template
if: (github.event_name == 'push' || github.event_name == 'schedule') && !startsWith(github.ref, 'refs/tags') && github.repository_owner == 'zenstruck'
runs-on: ubuntu-latest
steps:
- uses: zenstruck/.github@php-cs-fixer
with:
php: 8
key: ${{ secrets.GPG_PRIVATE_KEY }}
token: ${{ secrets.COMPOSER_TOKEN }}

sync-with-template:
name: Sync meta files
if: (github.event_name == 'push' || github.event_name == 'schedule') && !startsWith(github.ref, 'refs/tags') && github.repository_owner == 'zenstruck'
runs-on: ubuntu-latest
steps:
- uses: zenstruck/.github@sync-with-template
with:
key: ${{ secrets.GPG_PRIVATE_KEY }}
token: ${{ secrets.COMPOSER_TOKEN }}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/composer.lock
/phpunit.xml
/vendor/
/build/
/var/
/.php-cs-fixer.cache
/.phpunit.result.cache
14 changes: 14 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$file = __DIR__.'/.php-cs-fixer.temp.php';

\file_put_contents(
$file,
\file_get_contents('https://raw.githubusercontent.com/zenstruck/.github/main/.php-cs-fixer.dist.php')
);

try {
return require $file;
} finally {
\unlink($file);
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Kevin Bond

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "zenstruck/memoize",
"description": "Helper trait to efficiently cache expensive methods in memory.",
"homepage": "https://github.com/zenstruck/memoize",
"type": "library",
"license": "MIT",
"keywords": ["cache"],
"authors": [
{
"name": "Kevin Bond",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.0"
},
"require-dev": {
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5.0",
"symfony/phpunit-bridge": "^6.1",
"symfony/var-dumper": "^5.4|^6.0"
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"psr-4": { "Zenstruck\\": ["src/"] }
},
"autoload-dev": {
"psr-4": { "Zenstruck\\Tests\\": ["tests/"] }
},
"minimum-stability": "dev",
"prefer-stable": true
}
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 8
treatPhpDocTypesAsCertain: false
paths:
- src
# ignoreErrors:
# - '#no value type specified in iterable type array#'
31 changes: 31 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
</php>

<testsuites>
<testsuite name="zenstruck/memoize Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>src</directory>
</include>
</coverage>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
37 changes: 37 additions & 0 deletions src/Memoize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the zenstruck/memoize package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck;

use Zenstruck\Memoize\Cache;

/**
* @author Kevin Bond <[email protected]>
*/
trait Memoize
{
/**
* @template T
*
* @param callable():T $factory
*
* @return T
*/
protected function memoize(string $key, callable $factory): mixed
{
return Cache::getInstance()->get($this, $key, $factory);
}

protected function clearMemoized(?string $key = null): void
{
Cache::getInstance()->clear($this, $key);
}
}
68 changes: 68 additions & 0 deletions src/Memoize/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the zenstruck/memoize package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Memoize;

/**
* @author Kevin Bond <[email protected]>
*
* @internal
*/
final class Cache
{
private static self $instance;

/** @var \WeakMap<object,\ArrayObject<string,mixed>> */
private \WeakMap $cache;

private function __construct()
{
$this->cache = new \WeakMap();
}

public static function getInstance(): self
{
return self::$instance ??= new self();
}

/**
* @template T
*
* @param callable():T $factory
*
* @return T
*/
public function get(object $object, string $key, callable $factory): mixed
{
$array = $this->cache[$object] ??= new \ArrayObject();

if ($array->offsetExists($key)) {
return $array[$key];
}

return $array[$key] = $factory();
}

public function clear(object $object, ?string $key = null): void
{
if (!isset($this->cache[$object])) {
return;
}

if ($key) {
unset($this->cache[$object][$key]);

return;
}

unset($this->cache[$object]);
}
}
Loading

0 comments on commit e32fec4

Please sign in to comment.