namespace Akamai\Open\EdgeGrid;
public function __construct() $this->climate = new \League\CLImate\CLImate();
if ($this->parseArguments()) {
protected function parseArguments() $args = $this->getNamedArgs();
$this->climate->arguments->add($args);
if ($_SERVER['argc'] == 1) {
if ($this->climate->arguments->defined('help')) {
if ($this->climate->arguments->defined('version')) {
$this->climate->arguments->parse($_SERVER['argv']);
$padding = sizeof($args); foreach ($this->climate->arguments->toArray() as $arg) { $argSize = sizeof($_SERVER['argv']) - $padding - 1; for ($i = 0; $i < $argSize; $i++) { $this->climate->arguments->add($args); $this->climate->arguments->parse($_SERVER['argv']); } catch (\Exception $e) {
protected function executeCommand()
\Akamai\Open\EdgeGrid\Client::setDebug(true); \Akamai\Open\EdgeGrid\Client::setVerbose(true);
$args = $this->climate->arguments->all();
if ($this->climate->arguments->defined('auth-type')) { $auth = $this->climate->arguments->get('auth'); if ($this->climate->arguments->get('auth-type') == 'edgegrid' || (!$this->climate->arguments->defined('auth-type'))) { if ($this->climate->arguments->defined('auth')) { $section = (substr($auth, -1) == ':') ? substr($auth, 0, -1) : $auth; $client = Client::createFromEdgeRcFile($section);
if (in_array($this->climate->arguments->get('auth-type'), ['basic', 'digest'])) { if (!$this->climate->arguments->defined('auth') || $this->climate->arguments->get('auth') === null) {
$this->climate->arguments->get('auth-type')
if (strpos(':', $auth[0]) !== false) { list($auth[0], $auth[1]) = explode(':', $auth[0]);
$client = new Client(['auth' => $auth]);
foreach ($args as $arg) { if (empty($value) || is_bool($value) || $arg->longPrefix()) {
if (in_array(strtoupper($value), $methods)) {
if (!isset($url) && preg_match('@^(http(s?)://|:).*$@', trim($value))) {
if (preg_match('/^(?<key>.*?):=(?<file>@?)(?<value>.*?)$/', $value, $matches)) { if (!$value = $this->getArgValue($matches)) {
$body[$matches['key']] = json_decode($value);
if (preg_match('/^(?<header>.*?):(?<value>.*?)$/', $value, $matches) && !preg_match('@^http(s?)://@', $value)) { $options['headers'][$matches['header']] = $matches['value'];
if (preg_match('/^(?<key>.*?)=(?<file>@?)(?<value>.*?)$/', $value, $matches)) { if (!$value = $this->getArgValue($matches)) {
$body[$matches['key']] = $matches['value'];
$this->climate->error("Unknown argument: " . $value);
$fp = fopen('php://stdin', 'r'); stream_set_blocking($fp, false); if (!empty(trim($stdin))) {
if (!empty($stdin) && !empty($body)) { "error: Request body (from stdin or a file) and request data (key=value) cannot be mixed."
if (sizeof($body) && !$this->climate->arguments->defined('form')) { if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'application/json'; if (!isset($options['headers']['Accept'])) { $options['headers']['Accept'] = 'application/json'; $options['body'] = (!is_string($body)) ? json_encode($body) : $body;
if (sizeof($body) && $this->climate->arguments->defined('form')) { if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
$options['body'] = (!is_string($body)) ? http_build_query($body, null, null, PHP_QUERY_RFC1738) : $body;
$options['allow_redirects'] = false; if ($this->climate->arguments->defined('follow')) { $options['allow_redirects'] = true;
return $client->request($method, $url, $options);
$arguments = new \League\CLImate\Argument\Manager(); $arguments->description("Akamai {OPEN} Edgegrid Auth for PHP Client (v" .Client::VERSION. ')'); $arguments->add($this->getNamedArgs()); $arguments->usage($this->climate, $_SERVER['argv']);
public function version()
protected function getNamedArgs() 'description' => 'Show this help output', 'longPrefix' => 'auth-type', 'description' => "{basic, digest, edgegrid}" 'description' => '.edgerc section name, or user[:password]' 'description' => '(default) Data items from the command line are serialized as a JSON object.', 'longPrefix' => 'follow', 'description' => 'Set this flag if redirects are allowed', 'description' => 'Data items from the command line are serialized as form fields', 'longPrefix' => 'version', 'description' => 'Show version', 'description' => 'HTTP Method (default: GET)'
protected function getArgValue($matches) $value = $matches['value']; if (!empty($matches['file'])) { if (!file_exists($matches['value']) || !is_readable($matches['value'])) { $this->climate->error("Unable to read input file: " . $matches['value']); $value = file_get_contents($matches['value']);
|