28 changed files with 641 additions and 207 deletions
@ -0,0 +1,25 @@
|
||||
imports: |
||||
- php |
||||
|
||||
tools: |
||||
external_code_coverage: |
||||
timeout: 1000 |
||||
# PHP |
||||
|
||||
# Don't like PSR standards, not going to get messages for them! |
||||
php_code_sniffer: false |
||||
|
||||
php_sim: true |
||||
# Can't be used with similarity analyzer |
||||
php_cpd: false |
||||
|
||||
php_mess_detector: true |
||||
php_pdepend: true |
||||
php_loc: true |
||||
php_analyzer: |
||||
config: |
||||
metrics_lack_of_cohesion_methods: |
||||
enabled: true |
||||
doc_comment_fixes: |
||||
enabled: true |
||||
php_hhvm: true |
@ -0,0 +1,19 @@
|
||||
language: php |
||||
|
||||
php: |
||||
- 5.4 |
||||
- 5.5 |
||||
- 5.6 |
||||
- hhvm |
||||
|
||||
script: |
||||
- mkdir -p build/logs |
||||
- cd tests && phpunit --coverage-clover build/logs/clover.xml |
||||
|
||||
after_script: |
||||
- wget https://scrutinizer-ci.com/ocular.phar |
||||
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml |
||||
|
||||
matrix: |
||||
allow_failures: |
||||
- hhvm |
@ -0,0 +1,66 @@
|
||||
<?php |
||||
/** |
||||
* Sleepy - a REST framework |
||||
* |
||||
* |
||||
* A PHP Rest Framework valuing convention over configuration, |
||||
* but aiming to be as flexible as possible |
||||
* |
||||
* @author Timothy J. Warren |
||||
*/ |
||||
|
||||
namespace Sleepy\Core; |
||||
|
||||
/** |
||||
* Class for managing configuration values |
||||
*/ |
||||
class Config { |
||||
|
||||
/** |
||||
* The config array |
||||
* @var array |
||||
*/ |
||||
protected $data = []; |
||||
|
||||
/** |
||||
* Load the specified config file and return |
||||
* the config array |
||||
* |
||||
* @throws \InvalidArugmentException |
||||
* @param string $name |
||||
* @return array |
||||
*/ |
||||
public function load($name) |
||||
{ |
||||
$file = APPPATH . 'config/'. $name . '.php'; |
||||
|
||||
if (is_file($file)) |
||||
{ |
||||
$conf =require_once($file); |
||||
} |
||||
else |
||||
{ |
||||
throw new \InvalidArgumentException("The config file doesn't exist"); |
||||
} |
||||
|
||||
$this->data[$name] = $conf; |
||||
} |
||||
|
||||
/** |
||||
* Get the specific parameter from the specified file |
||||
* |
||||
* @param string $file |
||||
* @param string $key |
||||
* @return mixed |
||||
*/ |
||||
public function get($file, $key=NULL) |
||||
{ |
||||
if (is_null($key)) |
||||
{ |
||||
return $this->data[$file]; |
||||
} |
||||
|
||||
return $this->data[$file][$key]; |
||||
} |
||||
} |
||||
// End of Core/Config.php |
@ -0,0 +1,52 @@
|
||||
<?php |
||||
/** |
||||
* Sleepy - a REST framework |
||||
* |
||||
* |
||||
* A PHP Rest Framework valuing convention over configuration, |
||||
* but aiming to be as flexible as possible |
||||
* |
||||
* @author Timothy J. Warren |
||||
*/ |
||||
|
||||
namespace Sleepy\Traits; |
||||
|
||||
/** |
||||
* Trait to get rid of naive getter/setter boilerplate |
||||
*/ |
||||
trait GetSet |
||||
{ |
||||
|
||||
/** |
||||
* Dynamically create getters/setters |
||||
* |
||||
* @param string $func |
||||
* @param array $args |
||||
* @return mixed |
||||
*/ |
||||
public function __call($func, $args) |
||||
{ |
||||
$type = substr($func, 0, 3); |
||||
$val = strtolower(substr($func, 3)); |
||||
$valid_types = array( |
||||
'get' => 'get', |
||||
'set' => 'set' |
||||
); |
||||
|
||||
if ( ! property_exists($this, $val) || ! isset($valid_types[$type])) |
||||
{ |
||||
return NULL; |
||||
} |
||||
|
||||
// Auto-magical getters and setters |
||||
if ($type === 'get') |
||||
{ |
||||
return $this->$val; |
||||
} |
||||
elseif ($type === 'set') |
||||
{ |
||||
$this->$val = current($args); |
||||
} |
||||
} |
||||
} |
||||
// End of GetSet.php |
@ -0,0 +1,100 @@
|
||||
<?php |
||||
/** |
||||
* Sleepy - a REST framework |
||||
* |
||||
* |
||||
* A PHP Rest Framework valuing convention over configuration, |
||||
* but aiming to be as flexible as possible |
||||
* |
||||
* @author Timothy J. Warren |
||||
*/ |
||||
|
||||
namespace Sleepy\Type; |
||||
|
||||
use \Sleepy\Core\Abstracts\Type as aType; |
||||
|
||||
/** |
||||
* Implementation of XML data type |
||||
*/ |
||||
class XML extends aType { |
||||
|
||||
/** |
||||
* The mime type for output |
||||
* |
||||
* @var string |
||||
*/ |
||||
protected $mime = 'application/xml'; |
||||
|
||||
/** |
||||
* Convert php array/object to xml |
||||
* |
||||
* @param array|object $data |
||||
* @return string |
||||
*/ |
||||
public function serialize($data = NULL) |
||||
{ |
||||
$this->set_data($data); |
||||
|
||||
$xmlObject = new \SimpleXMLElement('<?xml version="1.0"?><root></root>');
|
||||
$this->array_to_xml($data, $xmlObject); |
||||
|
||||
return $xmlObject->asXML(); |
||||
} |
||||
|
||||
/** |
||||
* Convert xml to php data |
||||
* |
||||
* @param string $string |
||||
* @return object |
||||
*/ |
||||
public function unserialize($string) |
||||
{ |
||||
$xml = \simplexml_load_string($string); |
||||
$json = json_encode($xml); |
||||
$object = json_decode($json); |
||||
|
||||
return $object; |
||||
} |
||||
|
||||
/** |
||||
* Recursively generate xml from an array or object |
||||
* |
||||
* @param array|object $array |
||||
* @param \SimpleXMLElement $xmlObject |
||||
*/ |
||||
private function array_to_xml($array, \SimpleXMLElement &$xmlObject) |
||||
{ |
||||
foreach($array as $key => $val) |
||||
{ |
||||
$key = $this->fix_xml_key($key); |
||||
|
||||
if ( ! is_scalar($val)) |
||||
{ |
||||
$subnode = $xmlObject->addChild($key); |
||||
$this->array_to_xml($val, $subnode); |
||||
} |
||||
else |
||||
{ |
||||
$xmlObject->addChild($key, \htmlspecialchars($val, ENT_XML1, 'UTF-8')); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Make an invalid xml key more valid |
||||
* |
||||
* @param string $key |
||||
* @return string |
||||
*/ |
||||
private function fix_xml_key($key) |
||||
{ |
||||
if (is_numeric($key)) |
||||
{ |
||||
$key = "item_{$key}"; |
||||
} |
||||
|
||||
return preg_replace('`[^a-zA-Z0-9_:]`', '_', $key); |
||||
} |
||||
|
||||
} |
||||
// End of Type/XML.php |
@ -0,0 +1,40 @@
|
||||
<?php |
||||
/** |
||||
* Sleepy - a REST framework |
||||
* |
||||
* |
||||
* A PHP Rest Framework valuing convention over configuration, |
||||
* but aiming to be as flexible as possible |
||||
* |
||||
* @author Timothy J. Warren |
||||
* @package Sleepy |
||||
*/ |
||||
|
||||
namespace Sleepy; |
||||
|
||||
/** |
||||
* Autoloader |
||||
*/ |
||||
\spl_autoload_register(function($item) { |
||||
$path_items = \explode('\\', \ltrim($item, '\\')); |
||||
|
||||
// If the namespace is a straight mapping to the class, just load it |
||||
$simple_path = \implode('/', $path_items); |
||||
$file = BASEPATH . "{$simple_path}.php"; |
||||
|
||||
if (file_exists($file)) |
||||
{ |
||||
require_once($file); |
||||
return; |
||||
} |
||||
|
||||
// Check the application folder |
||||
$file = str_replace(SLEEPY_DIR, APP_DIR, $file); |
||||
if (file_exists($file)) |
||||
{ |
||||
require_once($file); |
||||
return; |
||||
} |
||||
}); |
||||
|
||||
// End of autoload.php |
@ -0,0 +1,26 @@
|
||||
<?php |
||||
/** |
||||
* Sleepy - a REST framework |
||||
* |
||||
* |
||||
* A PHP Rest Framework valuing convention over configuration, |
||||
* but aiming to be as flexible as possible |
||||
* |
||||
* @author Timothy J. Warren |
||||
* @package Sleepy |
||||
*/ |
||||
|
||||
return [ |
||||
|
||||
// -------------------------------------------------------------------------- |
||||
// Map mime types to type classes |
||||
// -------------------------------------------------------------------------- |
||||
'application/json' => 'JSON', |
||||
'text/yaml' => 'YAML', |
||||
'application/yaml' => 'YAML', |
||||
'text/html' => 'HTML', |
||||
'application/xhtml+xml' => 'HTML', |
||||
'*/*' => 'HTML' |
||||
]; |
||||
|
||||
// End of config/type_class_map.php |
@ -0,0 +1,28 @@
|
||||
<?php |
||||
|
||||
use Sleepy\Core\Input; |
||||
use Sleepy\Core\Config; |
||||
use Sleepy\Core\Output; |
||||
|
||||
class OutputTest extends Sleepy_Testcase { |
||||
|
||||
protected $output; |
||||
|
||||
public function setUp() |
||||
{ |
||||
parent::setUp(); |
||||
|
||||
$c = new Config(); |
||||
$i = new Input(); |
||||
|
||||
$this->output = new Output($c, $i); |
||||
} |
||||
|
||||
public function testGetAcceptedType() |
||||
{ |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
// End of OutputTest.php |
@ -1,17 +1,19 @@
|
||||
<?php |
||||
|
||||
require_once(BASEPATH . 'core/iType.php'); |
||||
require_once(BASEPATH . 'core/aType.php'); |
||||
|
||||
use Sleepy\core\iType; |
||||
use Sleepy\core\aType; |
||||
use Sleepy\Core\Interfaces\Type as iType; |
||||
use Sleepy\Core\Abstracts\Type as aType; |
||||
|
||||
class aTypeTest extends Sleepy_TestCase { |
||||
|
||||
public function testSanity() |
||||
{ |
||||
$this->assertEquals(['Sleepy\\core\\iType' => 'Sleepy\\core\\iType'], class_implements('Sleepy\\core\\aType')); |
||||
$this->assertEquals( |
||||
['Sleepy\\Core\\Interfaces\\Type' => 'Sleepy\\Core\\Interfaces\\Type'], |
||||
class_implements('Sleepy\\Core\\Abstracts\\Type') |
||||
); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
// End of aTypeTest.php |
@ -1,41 +1,40 @@
|
||||
<?php |
||||
|
||||
require_once(BASEPATH . 'core/iType.php'); |
||||
require_once(BASEPATH . 'core/aType.php'); |
||||
require_once(BASEPATH . 'types/JSON.php'); |
||||
|
||||
use Sleepy\core; |
||||
use Sleepy\types\JSON; |
||||
use Sleepy\execeptions; |
||||
use Sleepy\Type\JSON; |
||||
|
||||
class MockJSON extends JSON { |
||||
|
||||
|
||||
protected $mime = ''; |
||||
} |
||||
|
||||
class JSONTest extends Sleepy_Testcase { |
||||
|
||||
public function setUp() { |
||||
$this->JSON = new JSON([]); |
||||
$this->JSON = new JSON(['foo'=>'bar']); |
||||
} |
||||
|
||||
public function testSanity() { |
||||
$this->assertTrue(is_a($this->JSON, 'Sleepy\\types\\JSON')); |
||||
$this->assertTrue(is_a($this->JSON, 'Sleepy\\core\\aType')); |
||||
$this->assertEquals(['Sleepy\\core\\iType' => 'Sleepy\\core\\iType'], class_implements('Sleepy\\types\\JSON')); |
||||
$this->assertTrue(is_a($this->JSON, 'Sleepy\\Type\\JSON')); |
||||
$this->assertTrue(is_a($this->JSON, 'Sleepy\\Core\\Abstracts\\Type')); |
||||
$this->assertEquals( |
||||
['Sleepy\\Core\\Interfaces\\Type' => 'Sleepy\\Core\\Interfaces\\Type'], |
||||
class_implements('Sleepy\\Type\\JSON') |
||||
); |
||||
} |
||||
|
||||
public function testFunction() |
||||
|
||||
|
||||
|
||||
public function testNIE() { |
||||
try { |
||||
$json = new MockJSON([]); |
||||
} |
||||
catch (Sleepy\exceptions\NotImplementedException $e) { |
||||
catch (Sleepy\Exception\NotImplementedException $e) { |
||||
$this->assertTrue(TRUE); |
||||
} |
||||
} |
||||
|
||||
|
||||
public function testGetMime() |
||||
{ |
||||
$mime = $this->JSON->get_mime(); |
||||
$this->assertEquals('application/json', $mime); |
||||
} |
||||
} |
||||
// End of JSONTest |
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
use Sleepy\Type\XML; |
||||
|
||||
class MockXML extends XML { |
||||
|
||||
protected $mime = ''; |
||||
} |
||||
|
||||
class XMLTest extends Sleepy_Testcase { |
||||
|
||||
public function setUp() { |
||||
$test_array = [ |
||||
'foo' => 'bar', |
||||
'baz' => [ |
||||
0 => ['x' => 'y'] |
||||
] |
||||
]; |
||||
|
||||
$this->XML = new XML($test_array); |
||||
} |
||||
|
||||
public function testSanity() { |
||||
$this->assertTrue(is_a($this->XML, 'Sleepy\\Type\\XML')); |
||||
$this->assertTrue(is_a($this->XML, 'Sleepy\\Core\\Abstracts\\Type')); |
||||
$this->assertEquals( |
||||
['Sleepy\\Core\\Interfaces\\Type' => 'Sleepy\\Core\\Interfaces\\Type'], |
||||
class_implements('Sleepy\\Type\\XML') |
||||
); |
||||
} |
||||
|
||||
public function testNIE() { |
||||
try { |
||||
$xml = new MockXML([]); |
||||
} |
||||
catch (Sleepy\Exception\NotImplementedException $e) { |
||||
$this->assertTrue(TRUE); |
||||
} |
||||
} |
||||
|
||||
public function testSerialize() |
||||
{ |
||||
$xml = $this->XML->serialize(); |
||||
$xml_tostring = $this->XML->__toString(); |
||||
$expected = '<?xml version="1.0"?>' .
|
||||
"\n<root><foo>bar</foo><baz><item_0><x>y</x></item_0></baz></root>\n"; |
||||
|
||||
$this->assertEquals($expected, $xml); |
||||
$this->assertEquals($xml, $xml_tostring); |
||||
} |
||||
|
||||
public function testUnSerialize() |
||||
{ |
||||
$object = $this->XML->unserialize('<?xml version="1.0"?>' . "\n<root><foo>bar</foo></root>\n");
|
||||
|
||||
$expected = (object) ['foo' => 'bar']; |
||||
|
||||
$this->assertEquals($expected, $object); |
||||
} |
||||
|
||||
} |
||||
// End of XMLTest |
Reference in new issue