Skip to content

Commit

Permalink
Remove automagic (de-)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
brstgt authored and Benjamin Zikarsky committed Sep 18, 2017
1 parent 8e38b90 commit b422671
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 36 deletions.
13 changes: 8 additions & 5 deletions examples/partial-updates/async-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@
// on successful creation
function (ClientInterface $client) {
$hosts = ['google.com', 'facebook.com', 'github.com', 'wikipedia.org'];
$client->submit("ping", $hosts)->then(function (TaskInterface $task) {
printf("Pinging: %s [handle:%s]\n",
implode(", ", $task->getWorkload()),
$client->submit("ping", serialize($hosts))->then(function (TaskInterface $task) {
printf(
"Pinging: %s [handle:%s]\n",
implode(", ", unserialize($task->getWorkload())),
$task->getHandle()
);

$task->on('data', function (TaskDataEvent $event) {
echo "Partial update:\n";
print_r($event->getData());
print_r(unserialize($event->getData()));
echo "\n";
});

$task->on('complete', function (TaskDataEvent $event, ClientInterface $client) {
echo "Final result: \n";
print_r($event->getData());
print_r(unserialize($event->getData()));
echo "\n";
$client->disconnect();
});
});
Expand Down
2 changes: 1 addition & 1 deletion examples/partial-updates/async-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function (WorkerInterface $worker) use ($factory) {
$worker->setId('Test-Client/' . getmypid());
$worker->register('ping', function (JobInterface $job) {
$result = [];
$hosts = $job->getWorkload();
$hosts = unserialize($job->getWorkload());

$pingHost = function () use (&$hosts, &$result, $job, &$pingHost) {
$host = array_shift($hosts);
Expand Down
4 changes: 2 additions & 2 deletions examples/partial-updates/traditional-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
if (!@$worker->wait()) {
if ($worker->returnCode() == GEARMAN_NO_ACTIVE_FDS) {
# We are not connected to any servers, so wait a bit before
# trying to reconnect.
sleep(5);
# trying to reconnect.
sleep(5);
continue;
}
break;
Expand Down
5 changes: 3 additions & 2 deletions examples/simple/async-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Zikarsky\React\Gearman\TaskInterface;
use Zikarsky\React\Gearman\Factory;

require_once __DIR__ . "/../vendor/autoload.php";
require_once __DIR__ . "/../../vendor/autoload.php";

// use default options
$factory = new Factory();
Expand All @@ -14,7 +14,8 @@
// on successful creation
function (ClientInterface $client) {
$client->submit("reverse", "Hallo Welt!")->then(function (TaskInterface $task) {
printf("Submitted: %s with \"%s\" [handle: %s]\n",
printf(
"Submitted: %s with \"%s\" [handle: %s]\n",
$task->getFunction(),
$task->getWorkload(),
$task->getHandle()
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/async-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Zikarsky\React\Gearman\JobInterface;
use Zikarsky\React\Gearman\Factory;

require_once __DIR__ . "/../vendor/autoload.php";
require_once __DIR__ . "/../../vendor/autoload.php";

// use default options
$factory = new Factory();
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/traditional-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
if (!@$worker->wait()) {
if ($worker->returnCode() == GEARMAN_NO_ACTIVE_FDS) {
# We are not connected to any servers, so wait a bit before
# trying to reconnect.
sleep(5);
# trying to reconnect.
sleep(5);
continue;
}
break;
Expand Down
19 changes: 3 additions & 16 deletions src/Command/Binary/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,25 @@ public function set($key, $value)
throw new InvalidArgumentException($this->type->getName() . " does not have a $key argument");
}

if (!is_scalar($value)) {
$value = serialize($value);
}

$this->data[$key] = $value;
}

public function get($key, $default = null, $serialized = false)
public function get($key, $default = null)
{
if (!$this->type->hasArgument($key)) {
throw new InvalidArgumentException($this->type->getName() . " does not have a $key argument");
}

$data = isset($this->data[$key]) ? $this->data[$key] : $default;

if (false === $serialized && $key == self::DATA && self::isSerialized($data)) {
$data = unserialize($data);
}

return $data;
}

public function getAll($default = null, $serialized = false)
public function getAll($default = null)
{
$args = [];
foreach ($this->type->getArguments() as $arg) {
$args[$arg] = $this->get($arg, $default, $serialized);
$args[$arg] = $this->get($arg, $default);
}

return $args;
Expand All @@ -92,9 +84,4 @@ public function getMagic()
{
return $this->magic;
}

private static function isSerialized($data)
{
return $data == serialize(false) || @unserialize($data) !== false;
}
}
4 changes: 2 additions & 2 deletions src/Command/Binary/ReadBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ protected function handleBuffer($buffer)
{
$len = strlen($buffer);

// @codeCoverageIgnoreStart
// This is an internal safeguard which is not testable
// @codeCoverageIgnoreStart
// This is an internal safeguard which is not testable
if ($len != $this->requiredBytes) {
throw new InvalidArgumentException("Expected a string with length of $this->requiredBytes. Got $len bytes");
}
Expand Down
10 changes: 5 additions & 5 deletions src/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ interface JobInterface
const STATUS_FAILED = "failed";


/**
* Get the function-name of this job
*
* @return string
*/
/**
* Get the function-name of this job
*
* @return string
*/
public function getFunction();

/**
Expand Down

0 comments on commit b422671

Please sign in to comment.