-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yaml configuration files support added. See changelog for details.
- Loading branch information
1 parent
1256f7e
commit 6fd0992
Showing
7 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php namespace SleepingOwl\Apist\Yaml; | ||
|
||
use SleepingOwl\Apist\Apist; | ||
use SleepingOwl\Apist\Selectors\ApistSelector; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class Parser | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $methods = []; | ||
/** | ||
* @var array | ||
*/ | ||
protected $structures = []; | ||
/** | ||
* @var string | ||
*/ | ||
protected $file; | ||
|
||
/** | ||
* @param $file | ||
*/ | ||
function __construct($file) | ||
{ | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* @param Apist $resource | ||
*/ | ||
public function load(Apist $resource) | ||
{ | ||
$data = Yaml::parse($this->file); | ||
if (isset($data['baseUrl'])) | ||
{ | ||
$resource->setBaseUrl($data['baseUrl']); | ||
unset($data['baseUrl']); | ||
} | ||
foreach ($data as $method => $methodConfig) | ||
{ | ||
if ($method[0] === '_') | ||
{ | ||
# structure | ||
$this->structures[$method] = $methodConfig; | ||
} else | ||
{ | ||
# method | ||
$methodConfig['blueprint'] = $this->parseBlueprint($methodConfig['blueprint']); | ||
$this->methods[$method] = $methodConfig; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $blueprint | ||
* @return array | ||
*/ | ||
protected function parseBlueprint($blueprint) | ||
{ | ||
$callback = function (&$value) | ||
{ | ||
if ($value[0] === ':') | ||
{ | ||
# structure | ||
$structure = $this->getStructure($value); | ||
$value = $this->parseBlueprint($structure); | ||
return; | ||
} | ||
if (strpos($value, '|') === false) return; | ||
|
||
$parts = preg_split('/\s?\|\s?/', $value); | ||
$selector = array_shift($parts); | ||
$value = Apist::filter($selector); | ||
foreach ($parts as $part) | ||
{ | ||
$this->addCallbackToFilter($value, $part); | ||
} | ||
}; | ||
if ( ! is_array($blueprint)) | ||
{ | ||
$callback($blueprint); | ||
} else | ||
{ | ||
array_walk_recursive($blueprint, $callback); | ||
} | ||
return $blueprint; | ||
} | ||
|
||
/** | ||
* @param ApistSelector $filter | ||
* @param $callback | ||
*/ | ||
protected function addCallbackToFilter(ApistSelector $filter, $callback) | ||
{ | ||
$method = strtok($callback, '(),'); | ||
$arguments = []; | ||
while ($argument = strtok('(),')) | ||
{ | ||
$argument = trim($argument); | ||
if (preg_match('/^[\'"].*[\'"]$/', $argument)) | ||
{ | ||
$argument = substr($argument, 1, -1); | ||
} | ||
if ($argument[0] === ':') | ||
{ | ||
# structure | ||
$structure = $this->getStructure($argument); | ||
$argument = $this->parseBlueprint($structure); | ||
} | ||
$arguments[] = $argument; | ||
} | ||
$filter->addCallback($method, $arguments); | ||
} | ||
|
||
/** | ||
* @param $name | ||
* @return mixed | ||
*/ | ||
protected function getStructure($name) | ||
{ | ||
$structure = '_' . substr($name, 1); | ||
if ( ! isset($this->structures[$structure])) | ||
{ | ||
throw new \InvalidArgumentException("Structure '$structure' not found.'"); | ||
} | ||
return $this->structures[$structure]; | ||
} | ||
|
||
/** | ||
* @param $name | ||
* @return array | ||
*/ | ||
public function getMethod($name) | ||
{ | ||
if ( ! isset($this->methods[$name])) | ||
{ | ||
throw new \InvalidArgumentException("Method '$name' not found.'"); | ||
} | ||
$methodConfig = $this->methods[$name]; | ||
return $methodConfig; | ||
} | ||
|
||
/** | ||
* @param $method | ||
* @param $arguments | ||
* @return mixed | ||
*/ | ||
public function insertMethodArguments($method, $arguments) | ||
{ | ||
array_walk_recursive($method, function (&$value) use ($arguments) | ||
{ | ||
if ( ! is_string($value)) return; | ||
$value = preg_replace_callback('/\$(?<num>[0-9]+)/', function ($finded) use ($arguments) | ||
{ | ||
$argumentPosition = intval($finded['num']) - 1; | ||
return isset($arguments[$argumentPosition]) ? $arguments[$argumentPosition] : null; | ||
}, $value); | ||
}); | ||
return $method; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php namespace SleepingOwl\Apist\Yaml; | ||
|
||
class YamlApist extends \SleepingOwl\Apist\Apist | ||
{ | ||
/** | ||
* @var Parser | ||
*/ | ||
protected $parser; | ||
|
||
/** | ||
* @param array $options | ||
*/ | ||
function __construct($file = null, $options = []) | ||
{ | ||
if ( ! is_null($file)) | ||
{ | ||
$this->loadFromYml($file); | ||
} | ||
parent::__construct($options); | ||
} | ||
|
||
/** | ||
* Load method data from yaml file | ||
* @param $file | ||
*/ | ||
protected function loadFromYml($file) | ||
{ | ||
$this->parser = new Parser($file); | ||
$this->parser->load($this); | ||
} | ||
|
||
/** | ||
* @param $name | ||
* @param $arguments | ||
* @return array | ||
*/ | ||
function __call($name, $arguments) | ||
{ | ||
if (is_null($this->parser)) | ||
{ | ||
throw new \InvalidArgumentException("Method '$name' not found.'"); | ||
} | ||
$method = $this->parser->getMethod($name); | ||
$method = $this->parser->insertMethodArguments($method, $arguments); | ||
$httpMethod = isset($method['method']) ? strtoupper($method['method']) : 'GET'; | ||
$options = isset($method['options']) ? $method['options'] : []; | ||
return $this->request($httpMethod, $method['url'], $method['blueprint'], $options); | ||
} | ||
|
||
} |