/** * @return void */ private function runJobs(array $jobs) { foreach ($jobs as $job) { if (is_string($job) && preg_match('#^(https?|local|remote):(.+)#', $job, $m)) { if ($m[1] === 'local') { $out = @system($m[2], $code); $err = $code !== 0; } elseif ($m[1] === 'remote') { $out = $this->server->execute($m[2]); $err = FALSE; } else { $err = ($out = @file_get_contents($job)) === FALSE; } $this->logger->log("{$job}: {$out}"); if ($err) { throw new \RuntimeException("Error in job {$job}"); } } elseif (is_callable($job)) { if ($job($this->server, $this->logger, $this) === FALSE) { throw new \RuntimeException('Error in job'); } } else { throw new \InvalidArgumentException("Invalid job {$job}."); } } }
/** * @param array of string|callable * @return void */ private function runJobs(array $jobs) { foreach ($jobs as $job) { if (is_string($job) && preg_match('#^(https?|local|remote):\\s*(.+)#', $job, $m)) { $out = $err = NULL; if ($m[1] === 'local') { $out = @system($m[2], $code); $err = $code !== 0 ? "exit code {$code}" : NULL; } elseif ($m[1] === 'remote') { try { $out = $this->server->execute($m[2]); } catch (ServerException $e) { $err = $e->getMessage() ?: 'unknown error'; } } else { $out = Helpers::fetchUrl($job, $err); } $this->logger->log($job . ($out == NULL ? '' : ": {$out}")); // intentionally == if ($err) { throw new \RuntimeException('Job failed, ' . $err); } } elseif (is_callable($job)) { if ($job($this->server, $this->logger, $this) === FALSE) { throw new \RuntimeException('Job failed'); } } else { throw new \InvalidArgumentException("Invalid job {$job}."); } } }
/** * @param array of string|callable * @return void */ private function runJobs(array $jobs) { foreach ($jobs as $job) { if (is_string($job) && preg_match('#^(https?|local|remote):\\s*(.+)#', $job, $m)) { if ($m[1] === 'local') { $out = @system($m[2], $code); $err = $code !== 0; } elseif ($m[1] === 'remote') { try { $out = $this->server->execute($m[2]); } catch (ServerException $e) { $out = $e->getMessage(); } $err = isset($e); } else { $err = ($out = @file_get_contents($job)) === FALSE; } $this->logger->log($job . ($out == NULL ? '' : ": {$out}")); // intentionally == if ($err) { throw new \RuntimeException('Error in job'); } } elseif (is_callable($job)) { if ($job($this->server, $this->logger, $this) === FALSE) { throw new \RuntimeException('Error in job'); } } else { throw new \InvalidArgumentException("Invalid job {$job}."); } } }
public function testInsertUpdateDelete() { $select = 'SELECT query_id, param_1, param_2, param_3, result ' . 'FROM test_queries WHERE query_id = ?'; $this->assertTrue($this->stmt->prepare($select)); $this->stmt->organizeParams(array(99)); $this->assertTrue($this->stmt->execute()); $this->stmt->organizeResults(); $expected = array(); $this->assertEquals($expected, $this->stmt->fetch()); $insert = 'INSERT INTO test_queries ' . '(query_id, param_1, param_2, param_3, result) ' . 'VALUES (?, ?, ?, ?, ?) '; $values = array(99, 88, 'code_z', 0, 'query 99 issued'); $this->stmt->reset(); $this->stmt->prepare($insert); $this->stmt->organizeParams($values); $this->assertTrue($this->stmt->execute()); $this->stmt->reset(); $this->assertTrue($this->stmt->prepare($select)); $this->stmt->organizeParams(array(99)); $this->assertTrue($this->stmt->execute()); $this->stmt->organizeResults(); $result = $this->stmt->fetch(); $expected = array(array('query_id' => 99, 'param_1' => 88, 'param_2' => 'code_z', 'param_3' => 0, 'result' => 'query 99 issued')); $this->assertEquals($expected, $result); $update = 'Update test_queries ' . 'SET param_1=?, param_2=?, param_3=?, result=? ' . 'WHERE query_id = ?'; $values = array(22, 'code_bbc', 1, 'update query passed', 99); $this->stmt->reset(); $this->assertTrue($this->stmt->prepare($update)); $this->stmt->organizeParams($values); $this->assertTrue($this->stmt->execute()); $this->stmt->reset(); $this->assertTrue($this->stmt->prepare($select)); $this->stmt->organizeParams(array(99)); $this->assertTrue($this->stmt->execute()); $this->stmt->organizeResults(); $result = $this->stmt->fetch(); $expected = array(array('query_id' => 99, 'param_1' => 22, 'param_2' => 'code_bbc', 'param_3' => 1, 'result' => 'update query passed')); $this->assertEquals($expected, $result); $delete = 'DELETE FROM test_queries WHERE query_id = ?'; $this->stmt->reset(); $this->assertTrue($this->stmt->prepare($delete)); $this->stmt->organizeParams(array(99)); $this->assertTrue($this->stmt->execute()); $this->assertTrue($this->stmt->prepare($select)); $this->stmt->organizeParams(array(99)); $this->assertTrue($this->stmt->execute()); $this->stmt->organizeResults(); $expected = array(); $this->assertEquals($expected, $this->stmt->fetch()); $this->stmt->close(); }
/** * @dataProvider provideBasicSql * @return null */ public function testFetchBufferedResults($sql, $values) { $this->stmt->prepare($sql); $this->stmt->isPrepared(); $params = $this->stmt->normalizeParams($values); $this->stmt->bindParams($params); $this->stmt->execute(); $this->stmt->organizeResults(); $ok = $this->stmt->storeResults(); $results = $this->stmt->fetch(); $this->stmt->freeStoredResults(); $this->assertInternalType('array', $results); $expected = array(array('query_id' => 1, 'param_1' => 1, 'param_2' => 'code_a', 'param_3' => 0, 'result' => 'query issued')); $this->assertEquals($expected, $results); }
public function execute() { // Parse payload if (empty($this->payload)) { $this->payload = file_get_contents('php://input'); } if (is_string($this->payload)) { $this->payload = json_decode($this->payload, true); } // Check JSON format if (!is_array($this->payload)) { return $this->getResponse(array('error' => array('code' => -32700, 'message' => 'Parse error')), array('id' => null)); } // Handle batch request if (array_keys($this->payload) === range(0, count($this->payload) - 1)) { $responses = array(); foreach ($this->payload as $payload) { if (!is_array($payload)) { $responses[] = $this->getResponse(array('error' => array('code' => -32600, 'message' => 'Invalid Request')), array('id' => null)); } else { $server = new Server($payload); $response = $server->execute(); if ($response) { $responses[] = $response; } } } return empty($responses) ? '' : '[' . implode(',', $responses) . ']'; } // Check JSON-RPC format if (!isset($this->payload['jsonrpc']) || !isset($this->payload['method']) || !is_string($this->payload['method']) || $this->payload['jsonrpc'] !== '2.0' || isset($this->payload['params']) && !is_array($this->payload['params'])) { return $this->getResponse(array('error' => array('code' => -32600, 'message' => 'Invalid Request')), array('id' => null)); } // Procedure not found if (!isset(self::$procedures[$this->payload['method']])) { return $this->getResponse(array('error' => array('code' => -32601, 'message' => 'Method not found')), $this->payload); } $callback = self::$procedures[$this->payload['method']]; $params = array(); $reflection = new \ReflectionFunction($callback); if (isset($this->payload['params'])) { $parameters = $reflection->getParameters(); if (!$this->mapParameters($this->payload['params'], $parameters, $params)) { return $this->getResponse(array('error' => array('code' => -32602, 'message' => 'Invalid params')), $this->payload); } } $result = $reflection->invokeArgs($params); return $this->getResponse(array('result' => $result), $this->payload); }
/** * Handle batch request * * @access private * @return string */ private function handleBatchRequest() { $responses = array(); foreach ($this->payload as $payload) { if (!is_array($payload)) { $responses[] = $this->getResponse(array('error' => array('code' => -32600, 'message' => 'Invalid Request')), array('id' => null)); } else { $server = new Server($payload, $this->callbacks, $this->classes); $response = $server->execute(); if ($response) { $responses[] = $response; } } } return empty($responses) ? '' : '[' . implode(',', $responses) . ']'; }