/** * Run closure remotely or locally * * @param $closure * * @throws \Exception * * @return mixed */ public function run(\Closure $closure) { try { $closure = new SerializableClosure($closure); if (!$this->communicator->isConnected()) { $this->communicator->connect(); } $serialize = base64_encode(serialize($closure)); $output = $this->communicator->run(sprintf('php -r \'%s $c=unserialize(base64_decode("%s")); echo %s($c());\'', $this->getClosureUnserializerAsString(), $serialize, $this->stringifier->getSerializeFunctionName())); return $this->stringifier->toObject($output); } catch (\RuntimeException $e) { throw new ExecutorException('An error occurs when execution php on target host with the following message: ' . $e->getMessage()); } catch (\Exception $e) { // todo Manage exception throw $e; } }
/** * @test */ public function objectUnserializationShouldExpected() { $this->assertEquals(new stdClass(), $this->stringifier->toObject('O:8:"stdClass":0:{}')); }
/** * @test */ public function assocArrayUnserializationShouldExpected() { $this->assertEquals(array('a' => 'string'), $this->stringifier->toObject("array (\n 'a' => 'string',\n)")); }
/** * @test */ public function assocArrayUnserializationShouldExpected() { $this->assertEquals(array('a' => 'string'), $this->stringifier->toObject('{"a":"string"}')); }