1
0
mirror of https://github.com/danog/amp.git synced 2025-01-21 21:01:16 +01:00

Refactor Registry for Drivers

This commit is contained in:
Aaron Piotrowski 2016-05-27 10:45:45 -05:00
parent 104b761e59
commit 4908813e08
3 changed files with 69 additions and 35 deletions

View File

@ -7,8 +7,6 @@ use Interop\Async\Loop\DriverFactory;
final class Loop
{
use Loop\Registry;
/**
* @var DriverFactory
*/
@ -45,10 +43,8 @@ final class Loop
if ($factory === null) {
self::$driver = null;
self::$registry = null;
} else {
self::$driver = self::createDriver();
self::$registry = [];
}
}
@ -62,13 +58,11 @@ final class Loop
*/
public static function execute(callable $callback, Driver $driver = null)
{
$previousRegistry = self::$registry;
$previousDriver = self::$driver;
$driver = $driver ?: self::createDriver();
self::$driver = $driver;
self::$registry = [];
self::$level++;
try {
@ -77,7 +71,6 @@ final class Loop
self::$driver->run();
} finally {
self::$driver = $previousDriver;
self::$registry = $previousRegistry;
self::$level--;
}
}
@ -281,6 +274,37 @@ final class Loop
self::get()->unreference($watcherId);
}
/**
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
* information is package private. Packages MUST NOT retrieve the stored state of other packages.
*
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
*
* @param string $key namespaced storage key
* @param mixed $value the value to be stored
*
* @return void
*/
public static function storeState($key, $value)
{
self::get()->storeState($key, $value);
}
/**
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
* the stored state of other packages.
*
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
*
* @param string $key namespaced storage key
*
* @return mixed previously stored value or null if it doesn't exist
*/
public static function fetchState($key)
{
return self::get()->fetchState($key);
}
/**
* Set a callback to be executed when an error occurs.
*

View File

@ -149,6 +149,31 @@ interface Driver
* @return void
*/
public function unreference($watcherId);
/**
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
* information is package private. Packages MUST NOT retrieve the stored state of other packages.
*
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
*
* @param string $key namespaced storage key
* @param mixed $value the value to be stored
*
* @return void
*/
public function storeState($key, $value);
/**
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
* the stored state of other packages.
*
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
*
* @param string $key namespaced storage key
*
* @return mixed previously stored value or null if it doesn't exist
*/
public function fetchState($key);
/**
* Set a callback to be executed when an error occurs.

View File

@ -3,62 +3,47 @@
namespace Interop\Async\Loop;
/**
* State registry to be used in Interop\Async\Loop.
*
* THIS TRAIT SHOULD NOT BE USED BY LOOP DRIVERS. It's the responsibility of the
* loop accessor to manage this state.
* State registry to be used by classes implementing the Driver interface.
*/
trait Registry
{
/**
* @var array
*/
private static $registry = null;
private $registry = [];
/**
* Stores information in the loop bound registry. This can be used to store
* loop bound information. Stored information is package private.
* Packages MUST NOT retrieve the stored state of other packages.
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
* information is package private. Packages MUST NOT retrieve the stored state of other packages.
*
* Therefore packages SHOULD use the following prefix to keys:
* `vendor.package.`
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
*
* @param string $key namespaced storage key
* @param mixed $value the value to be stored
*
* @return void
*/
public static function storeState($key, $value)
public function storeState($key, $value)
{
if (self::$registry === null) {
throw new \RuntimeException('Not within the scope of an event loop driver');
}
if ($value === null) {
unset(self::$registry[$key]);
unset($this->registry[$key]);
} else {
self::$registry[$key] = $value;
$this->registry[$key] = $value;
}
}
/**
* Fetches information stored bound to the loop. Stored information is
* package private. Packages MUST NOT retrieve the stored state of
* other packages.
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
* the stored state of other packages.
*
* Therefore packages SHOULD use the following prefix to keys:
* `vendor.package.`
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
*
* @param string $key namespaced storage key
*
* @return mixed previously stored value or null if it doesn't exist
*/
public static function fetchState($key)
public function fetchState($key)
{
if (self::$registry === null) {
throw new \RuntimeException('Not within the scope of an event loop driver');
}
return isset(self::$registry[$key]) ? self::$registry[$key] : null;
return isset($this->registry[$key]) ? $this->registry[$key] : null;
}
}