Skip to content
This repository was archived by the owner on Jul 8, 2018. It is now read-only.

Commit beb4e8c

Browse files
committed
Initial commit
0 parents  commit beb4e8c

12 files changed

+486
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
sudo: false
2+
3+
language: php
4+
5+
php:
6+
- 7.0
7+
- nightly
8+
9+
matrix:
10+
allow_failures:
11+
- php: nightly
12+
13+
env:
14+
- PHP_WEAK_VERSION=master
15+
- PHP_WEAK_VERSION=v0.1.0
16+
17+
before_install:
18+
- sh install_php_weak_ext.sh ${PHP_WEAK_VERSION}
19+
- echo 'extension = weak.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/weak.ini
20+
21+
before_script:
22+
- composer self-update
23+
- composer install
24+
25+
script:
26+
- vendor/bin/phpunit --coverage-text --configuration phpunit.xml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2016 Bogdan Padalko <zaq178miami@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Weak-referenced data structures for PHP [![Build Status](https://travis-ci.org/pinepain/php-weak-lib.svg)](https://travis-ci.org/pinepain/php-weak-lib)
2+
3+
This library base on [php-weak][php-weak-ext] PHP extension and provides various weak data types.
4+
5+
Currently only weak version of [`SplObjectStorage`][php-SplObjectStorage] provided, which is basically a
6+
[`WeakMap`][js-WeakMap] in terms of ECMA 2015, but for PHP.
7+
8+
## About:
9+
10+
`Weak\SplObjectStorage` aims to be a simple drop-in replacement for SPL `SplObjectStorage` when you need to remove
11+
key (and it optional value) after key object GCed. This is useful when you want to organize some runtime caching or
12+
doing components integrations which converts one object into another and have to keep transformation table of one into
13+
another. But basically it is `WeakMap` or `WeakHashMap`, depends of what languages you are familiar with. If you don't
14+
set any value for object than it will be just a `WeakSet` for you.
15+
16+
In short: when key object GCed, it will be removed from `Weak\SplObjectStorage`
17+
18+
## Installation:
19+
20+
`composer require pinepain/php-weak-lib`
21+
22+
## Example:
23+
24+
```php
25+
<?php
26+
27+
require __DIR__ . '/../vendor/autoload.php';
28+
29+
use Weak\SplObjectStorage;
30+
31+
$storage = new SplObjectStorage();
32+
33+
$obj = new stdClass();
34+
$obj->obj = 'I am object';
35+
36+
$inf = new stdClass();
37+
$inf->inf = 'I am info';
38+
39+
$storage->attach($obj, $inf);
40+
41+
var_dump($storage->count()); // 1
42+
43+
// Let's destroy object
44+
$obj = null;
45+
46+
var_dump($storage->count()); // 0
47+
```
48+
49+
## License
50+
51+
[php-weak-lib](https://github.com/pinepain/php-weak-lib) PHP library is a free software licensed under the [MIT license](http://opensource.org/licenses/MIT).
52+
53+
[php-weak-ext]: https://github.com/pinepain/php-weak
54+
[php-SplObjectStorage]: http://php.net/manual/en/class.splobjectstorage.php
55+
[js-WeakMap]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/WeakMap

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "pinepain/php-weak-lib",
3+
"description": "Weak-referenced data structures for PHP",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"php-weak",
8+
"weak",
9+
"reference",
10+
"weakref",
11+
"weakreference",
12+
"weak data types",
13+
"data structure"
14+
],
15+
"authors": [
16+
{
17+
"name": "Bogdan Padalko",
18+
"email": "pinepain@gmail.com",
19+
"homepage": "https://github.com/pinepain"
20+
}
21+
],
22+
"require": {
23+
"php": "~7.0",
24+
"ext-weak": "~0.1.0"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^5.1",
28+
"pinepain/php-weak-stubs": "~0.1.0"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Weak\\": "./src"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Weak\\Tests\\": "./tests"
38+
}
39+
}
40+
}

install_php_weak_ext.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo Installing php-weak PHP extension ...
6+
7+
8+
PHP_WEAK_VERSION=$1
9+
10+
cd $HOME
11+
12+
if [ ! -d "$HOME/php-weak" ]; then
13+
git clone https://github.com/pinepain/php-weak.git
14+
else
15+
echo 'Using cached directory.';
16+
cd $HOME/php-weak
17+
git fetch
18+
fi
19+
20+
cd $HOME/php-weak
21+
git checkout ${PHP_WEAK_VERSION}
22+
23+
phpize --clean && phpize && ./configure && make
24+
25+
make test
26+
make install

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory suffix="Test.php">./tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist processUncoveredFilesFromWhitelist="true">
19+
<directory suffix=".php">./src</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/HashedReference.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the pinepain/php-weak PHP extension.
5+
*
6+
* Copyright (c) 2016 Bogdan Padalko <zaq178miami@gmail.com>
7+
*
8+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT
12+
*/
13+
14+
namespace Weak;
15+
16+
17+
class HashedReference extends Reference
18+
{
19+
private $hash;
20+
21+
public function __construct($referent, callable $notify = null, callable $hash_function = null)
22+
{
23+
parent::__construct($referent, $notify);
24+
25+
//$this->hash = ($hash_function ?? 'spl_object_hash')($referent);
26+
27+
if ($hash_function) {
28+
$this->hash = $hash_function($referent);
29+
} else {
30+
$this->hash = spl_object_hash($referent);
31+
}
32+
}
33+
34+
public function getHash() : string
35+
{
36+
return $this->hash;
37+
}
38+
}

src/SplObjectStorage.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the pinepain/php-weak PHP extension.
5+
*
6+
* Copyright (c) 2016 Bogdan Padalko <zaq178miami@gmail.com>
7+
*
8+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT
12+
*/
13+
14+
namespace Weak;
15+
16+
class SplObjectStorage extends \SplObjectStorage
17+
{
18+
/** {@inheritdoc} */
19+
public function attach($object, $data = null)
20+
{
21+
$object = new HashedReference($object, [$this, 'detach'], 'spl_object_hash');
22+
23+
parent::attach($object, $data);
24+
}
25+
26+
/** {@inheritdoc} */
27+
public function current()
28+
{
29+
$current = parent::current();
30+
31+
if ($current) {
32+
$current = $current->get();
33+
}
34+
35+
return $current;
36+
}
37+
38+
/** {@inheritdoc} */
39+
public function getHash($object)
40+
{
41+
if ($object instanceof HashedReference) {
42+
return $object->getHash();
43+
}
44+
45+
return parent::getHash($object);
46+
}
47+
}

tests/HashedReferenceTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the pinepain/php-weak PHP extension.
5+
*
6+
* Copyright (c) 2016 Bogdan Padalko <zaq178miami@gmail.com>
7+
*
8+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT
12+
*/
13+
14+
namespace Weak\Tests;
15+
16+
use PHPUnit_Framework_TestCase;
17+
18+
use Weak\HashedReference;
19+
20+
use stdClass;
21+
22+
class HashedReferenceTest extends PHPUnit_Framework_TestCase
23+
{
24+
public function test()
25+
{
26+
$obj = new stdClass();
27+
$obj_hash = 'test_hash';
28+
29+
$n = $this->getMock('stdClass', ['notify', 'hash']);
30+
31+
$n->expects($this->once())
32+
->method('notify');
33+
34+
35+
$n->expects($this->once())
36+
->method('hash')
37+
->willReturn($obj_hash);
38+
39+
$notify = function () use (&$n) {
40+
return $n->notify();
41+
};
42+
43+
$hash = function () use (&$n) {
44+
return $n->hash();
45+
};
46+
47+
$wr = new HashedReference($obj, $notify, $hash);
48+
49+
$this->assertSame($obj, $wr->get());
50+
$this->assertTrue($wr->valid());
51+
$this->assertSame($obj_hash, $wr->getHash());
52+
53+
$obj = null;
54+
55+
$this->assertNull($wr->get());
56+
$this->assertFalse($wr->valid());
57+
$this->assertSame($obj_hash, $wr->getHash());
58+
}
59+
}

0 commit comments

Comments
 (0)