Skip to content

Commit

Permalink
Merge branch 'release-0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
trq committed Mar 4, 2012
2 parents 8177e89 + 07d28d7 commit f433944
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 104 deletions.
Binary file modified build/proem.phar
Binary file not shown.
63 changes: 1 addition & 62 deletions lib/Proem/Api/Dispatch/Payload.php
Expand Up @@ -35,7 +35,7 @@
/**
* Proem\Api\Dispatch\Payload
*/
class Payload
class Payload extends KeyValStore
{
/**
* A flag to keep note as to wether or not this Payload is populated
Expand All @@ -44,67 +44,6 @@ class Payload
*/
private $populated = false;

/**
* Store the actual data.
*
* @var array
*/
private $data = array();

public function __construct()
{
$this->data = new KeyValStore;
}

/**
* Store a parameter.
*
* @param string $name
* @param string|array $value
* @return Command
*/
public function setParam($name, $value)
{
$this->data->set($name, $value);
return $this;
}

/**
* Store multiple params.
*
* @param array $params
*/
public function setParams(array $params)
{
foreach ($params as $index => $value) {
$this->setParam($index, $value);
}

return $this;
}

/**
* Retrieve a parameter or an optional default.
*
* @param string $name
* @param mixed $default
* @return mixed
*/
public function getParam($name, $default = null)
{
return $this->data->get($name, $default);
}

/**
* Retrieve all parameters as KeyValStore.
*
* @return array
*/
public function getParams()
{
return $this->data;
}

/**
* Is the Payload Populated?
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Proem/Api/Proem.php
Expand Up @@ -49,7 +49,7 @@ class Proem
/**
* Store the framework version
*/
const VERSION = '0.1.5';
const VERSION = '0.2.0';

/**
* Store events
Expand Down
4 changes: 2 additions & 2 deletions lib/Proem/Api/Routing/Route/Standard.php
Expand Up @@ -151,9 +151,9 @@ function($matches) use ($custom_filters, $default_tokens, $default_filters)
// parse it into an array and send it to setParams() instead
// of the singular setParam.
if ($key == 'params' && strpos($value, '/') !== false) {
$this->getPayload()->setParams($this->createAssocArray(explode('/', trim($value, '/'))));
$this->getPayload()->set($this->createAssocArray(explode('/', trim($value, '/'))));
} else {
$this->getPayload()->setParam($key, $value);
$this->getPayload()->set($key, $value);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Proem/Api/Service/Asset/Generic.php
Expand Up @@ -164,12 +164,12 @@ public function set($provides, \Closure $closure)
* Retrieve an instantiated Asset.
*
* Here the closure is passed this asset container and optionally the
* Proem\Api\Asset\Manager.
* Proem\Api\Service\Manager.
*
* This provides the closure with the ability to use any required parameters
* and also be able to call upon any other assets stored in the asset manager.
*
* @param Proem\Api\Asset\Manager $assetManager
* @param Proem\Api\Service\Manager $assetManager
*/
public function get(Manager $assetManager = null)
{
Expand Down
11 changes: 9 additions & 2 deletions lib/Proem/Api/Service/Manager.php
Expand Up @@ -64,7 +64,7 @@ class Manager
* Store an Asset container by named index.
*
* @param string $index The index the asset will be referenced by.
* @param Proem\Api\Asset $asset
* @param Proem\Api\Service\Asset\Generic $asset
*/
public function set($index, Asset $asset)
{
Expand Down Expand Up @@ -110,7 +110,14 @@ public function has($index)
*/
public function provides($index, $provides = null)
{
if ($provides === null) {
if (is_array($index)) {
foreach ($index as $key) {
if (!in_array($key, $this->provides)) {
return false;
}
}
return true;
} elseif ($provides === null) {
return in_array($index, $this->provides);
} else {
if ($this->has($index)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Proem/Api/Signal/Manager.php
Expand Up @@ -122,14 +122,14 @@ public function trigger(Array $options)
foreach ($this->queues[$ops->name] as $event) {
$eventObj = $ops->event;
if ($eventObj instanceof \Proem\Signal\Event\Generic) {
if (isset($ops->params)) {
if ($ops->has('params')) {
$eventObj->setParams($ops->params);
}
}
$eventObj->setTarget($ops->target);
$eventObj->setMethod($ops->method);
if ($return = $event($eventObj)) {
if (isset($ops->callback)) {
if ($ops->has('callback')) {
(new Callback($ops->callback, $return))->call();
}
}
Expand Down
85 changes: 78 additions & 7 deletions lib/Proem/Api/Util/Opt/Option.php
Expand Up @@ -30,7 +30,9 @@
*/
namespace Proem\Api\Util\Opt;

use Proem\Util\Process\Callback;
use Proem\Util\Process\Callback,
Proem\Service\Asset\Generic as GenericAsset,
Proem\Service\Manager as ServiceManager;

/**
* Proem\Api\Util\Opt\Option
Expand All @@ -40,8 +42,10 @@ class Option
private $value;
private $is_required = false;
private $is_type;
private $is_asset;
private $is_object;
private $is_classof;
private $throws = null;
private $unless = [];
private $type_validators = [];

Expand Down Expand Up @@ -112,6 +116,10 @@ public function required() {
return $this;
}

public function isRequired() {
return $this->is_required;
}

/**
* Disable this Option from being required if some other argument(s) has been provided
*
Expand Down Expand Up @@ -155,6 +163,26 @@ public function object($object)
return $this;
}

/**
* Force this Option's value to be an Asset or an ServiceMananger
* providing a specific object.
*
* @param $provides The Asset this Option provides
*/
public function asset($provides)
{
$this->is_asset = $provides;
return $this;
}

/**
* The exception to throw if invalid
*/
public function throws(callable $exception) {
$this->throws = $exception;
return $this;
}

/**
* Force this Option's value to be a string representation of a
* particular class or subclass
Expand Down Expand Up @@ -186,34 +214,77 @@ public function validate($options = []) {

if ($this->is_required) {
if ($this->value === __FILE__) {
throw new \InvalidArgumentException(' is a required option');
if ($this->throws === null) {
throw new \InvalidArgumentException(' is a required option');
} else {
throw (new Callback($this->throws))->call();
}
}
}

if ($this->is_type && $this->value !== __FILE__) {
if (isset($this->type_validators[$this->is_type])) {
//$func = $this->type_validators[$this->is_type];
if (!(new Callback($this->type_validators[$this->is_type], [$this->value]))->call()) {
throw new \InvalidArgumentException(' did not pass the "' . $this->is_type . '" validator');
if ($this->throws === null) {
throw new \InvalidArgumentException(' did not pass the "' . $this->is_type . '" validator');
} else {
throw (new Callback($this->throws))->call();
}
}
} else {
throw new \RuntimeException('No validator found for type ' . $this->is_type);
}
}

if ($this->is_asset && $this->value !== __FILE__) {
if ($this->value instanceof GenericAsset) {
if ($this->value->provides() !== $this->is_asset) {
if ($this->throws === null) {
throw new \InvalidArgumentException(' did not pass the "' . $this->is_asset . '" Asset validator');
} else {
throw (new Callback($this->throws))->call();
}
}
} elseif ($this->value instanceof ServiceManager) {
if (!$this->value->provides($this->is_asset)) {
if ($this->throws === null) {
throw new \InvalidArgumentException(' did not pass the "' . (is_array($this->is_asset) ? '[' . implode(', ', $this->is_asset) . ']' : $this->is_asset) . '" Asset validator');
} else {
throw (new Callback($this->throws))->call();
}
}
} else {
if ($this->throws === null) {
throw new \InvalidArgumentException(' is not a valid "Asset" or "Service\Manager"');
} else {
throw (new Callback($this->throws))->call();
}
}
}

if ($this->is_object && $this->value !== __FILE__) {
if (!$this->value instanceof $this->is_object) {
throw new \InvalidArgumentException(' is required to be an instance of ' . $this->is_object . ', ' . get_class($this->value) . ' provided');
if ($this->throws === null) {
throw new \InvalidArgumentException(' is required to be an instance of ' . $this->is_object . ', ' . (is_object($this->value) ? get_class($this->value) : $this->value) . ' provided');
} else {
throw (new Callback($this->throws))->call();
}
}
}

if ($this->is_classof && $this->value !== __FILE__) {
if (!$this->value == $this->is_classof && !is_subclass_of($this->is_classof, $this->value)) {
throw new \InvalidArgumentException(' is required to be a string representation of the class of type ' . $this->is_classof);
if ($this->throws === null) {
throw new \InvalidArgumentException(' is required to be a string representation of the class of type ' . $this->is_classof);
} else {
throw (new Callback($this->throws))->call();
}
}
}

if ($this->value == __FILE__) { $this->value = null; }
if ($this->value == __FILE__) {
return false;
}

return true;
}
Expand Down
11 changes: 8 additions & 3 deletions lib/Proem/Api/Util/Opt/Options.php
Expand Up @@ -44,6 +44,8 @@ trait Options
*/
public function setOptions($defaults, $options)
{
$payload = new Payload;

foreach ($options as $key => $value) {
if (isset($defaults[$key]) && ($defaults[$key] instanceof Option)) {
$defaults[$key]->setValue($value);
Expand All @@ -55,15 +57,18 @@ public function setOptions($defaults, $options)
foreach ($defaults as $key => $value) {
if ($value instanceof Option) {
try {
$value->validate($options);
$defaults[$key] = $value->getValue();
if ($value->isRequired() || $value->getValue() !== null) {
if ($value->validate($options)) {
$payload->set($key, $value->getValue());
}
}
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException($key . $e->getMessage());
} catch (\RuntimeException $e) {
throw new \RuntimeException($e->getMessage());
}
}
}
return (object) $defaults;
return $payload;
}
}
41 changes: 41 additions & 0 deletions lib/Proem/Api/Util/Opt/Payload.php
@@ -0,0 +1,41 @@
<?php

/**
* The MIT License
*
* Copyright (c) 2010 - 2012 Tony R Quilkey <trq@proemframework.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


/**
* @namespace Proem\Api\Util\Opt
*/
namespace Proem\Api\Util\Opt;

use Proem\Util\Storage\KeyValStore;

/**
* Proem\Api\Util\Opt\Payload
*/
class Payload extends KeyValStore
{

}

0 comments on commit f433944

Please sign in to comment.