This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pinepain/eliminate_duplication
Add behavior logic to WeakKeyValueMap and use inheritance
- Loading branch information
Showing
9 changed files
with
217 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tools: | ||
external_code_coverage: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the pinepain/php-weak-lib PHP library. | ||
* | ||
* Copyright (c) 2016 Bogdan Padalko <[email protected]> | ||
* | ||
* Licensed under the MIT license: http://opensource.org/licenses/MIT | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace Weak; | ||
|
||
use RuntimeException; | ||
use SplObjectStorage; | ||
|
||
abstract class AbstractWeakMap extends SplObjectStorage | ||
{ | ||
const WEAK_KEY = 1; | ||
const WEAK_VAL = 2; | ||
|
||
protected $behavior = 0; | ||
|
||
/** {@inheritdoc} */ | ||
public function attach($object, $data = null) | ||
{ | ||
if ($this->behavior & self::WEAK_KEY) { | ||
$object = $this->buildWeakKey($object); | ||
} | ||
|
||
if ($this->behavior & self::WEAK_VAL) { | ||
$data = $this->buildWeakValue($object, $data); | ||
} | ||
|
||
parent::attach($object, $data); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function current() | ||
{ | ||
$object = parent::current(); | ||
|
||
if ($this->behavior & self::WEAK_KEY) { | ||
$object = $this->extractWeakObject($object); | ||
} | ||
|
||
/* In some rare cases (it should never happen normally) orphaned weak reference may occurs in storage, so | ||
* so current object here MAY be null | ||
*/ | ||
|
||
return $object; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function addAll($storage) | ||
{ | ||
foreach ($storage as $obj) { | ||
$this->attach($obj, $storage[$obj]); | ||
} | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function removeAllExcept($storage) | ||
{ | ||
$pending_removal = new \SplObjectStorage(); | ||
|
||
foreach ($this as $obj) { | ||
if (!$storage->contains($obj)) { | ||
$pending_removal->attach($obj); | ||
} | ||
} | ||
|
||
$this->removeAll($pending_removal); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getHash($object) | ||
{ | ||
if ($this->behavior & self::WEAK_KEY) { | ||
if ($object instanceof HashedReference) { | ||
return $object->getHash(); | ||
} | ||
} | ||
|
||
return parent::getHash($object); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getInfo() | ||
{ | ||
$info = parent::getInfo(); | ||
|
||
if ($this->behavior & self::WEAK_VAL) { | ||
$info = $this->extractWeakObject($info); | ||
} | ||
|
||
return $info; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function setInfo($data) | ||
{ | ||
$object = $this->current(); | ||
|
||
if (!$object) { | ||
return; // nothing to do | ||
} | ||
|
||
if ($this->behavior & self::WEAK_VAL) { | ||
$data = $this->buildWeakValue($object, $data); | ||
} | ||
|
||
parent::setInfo($data); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function offsetSet($object, $data = null) | ||
{ | ||
$this->attach($object, $data); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function offsetGet($object) | ||
{ | ||
$info = parent::offsetGet($object); | ||
|
||
if ($this->behavior & self::WEAK_VAL) { | ||
$info = $this->extractWeakObject($info); | ||
} | ||
|
||
return $info; | ||
} | ||
|
||
protected function validateInfo($data) | ||
{ | ||
if (!is_object($data)) { | ||
throw new RuntimeException(static::class . ' expects data to be object, ' . gettype($data) . ' given'); | ||
} | ||
} | ||
|
||
protected function extractWeakObject($object) | ||
{ | ||
if ($object instanceof Reference) { | ||
$object = $object->get(); | ||
} | ||
|
||
return $object; | ||
} | ||
|
||
protected function buildWeakKey($object) | ||
{ | ||
$object = new HashedReference($object, [$this, 'detach'], 'spl_object_hash'); | ||
|
||
return $object; | ||
} | ||
|
||
protected function buildWeakValue($object, $data) | ||
{ | ||
$this->validateInfo($data); | ||
|
||
$data = new Reference($data, function () use ($object) { | ||
$this->detach($object); | ||
}); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.