Skip to content

Commit

Permalink
Added replica and beancan
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor de Mooij, Buurtnerd committed Aug 28, 2010
1 parent ad546fe commit 0cbb0e2
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 1 deletion.
163 changes: 163 additions & 0 deletions RedBean/BeanCan.php
@@ -0,0 +1,163 @@
<?php
/**
* BeanCan
* A Server Interface for RedBean and Fuse.
*
* The BeanCan Server is a lightweight, minimalistic server interface for
* RedBean that can perfectly act as an ORM middleware solution or a backend
* for an AJAX application.
*
* By Gabor de Mooij
*
*/
class BeanCan {

/**
* Holds a FUSE instance.
* @var RedBean_ModelHelper
*/
private $modelHelper;

/**
* Constructor.
*/
public function __construct() {
$this->modelHelper = new RedBean_ModelHelper;
}


/**
* Processes a batch request.
* The Bean Can Server only works with JSON (Javascript Object Notation).
* XML is not supported.
*
* A batch for the BeanCan Server should have the following format:
*
* {
* "label1": REQUEST,
* "label2": REQUEST
*
* ...etc...
* }
*
* The output from a BeanCan server looks like this:
*
* {
* "label1": OUTPUT,
* "label2": OUTPUT
* }
*
* If an error occurs and the Bean Can Server has no way
* to handle this error it will return a tiny JSON string:
*
* {
* "error":"<ERRORMESSAGESTRING>"
* }
*
* @param string $jsonBatch
*/
public function processBatch( $jsonString ) {

//Decode the JSON string using PHP native JSON parser.
$jsonArray = json_decode( $jsonString, true );

//Prepare output array for response.
$outputArray = array();

//Iterate over the request batch.
foreach($jsonArray as $bucketLabel=>$bucket) {

//Execute request and store result under label.
$outputArray[ $bucketLabel ] = $this->processRequest( $bucket );

}

//Output the JSON string directly
die( json_encode($outputArray) );
}


/**
* Processes a JSON object request.
*
* The format of the JSON Object string should be:
*
* {
* "bean":"<NAME OF THE BEAN YOU WANT TO INTERACT WITH>",
* "action":"<ACTION>",
* "data":"<DATA>"
* }
*
* If Action is: "store","load" OR "trash" the data will be
* imported in the bean, the bean will be passed to the R facade
* and the action will be invoked on the R facade.
*
* If the Action is different, the BeanCan will find the model
* associated with the bean and invoke the corresponding method
* on the model passing the data as arguments.
*
*
* @param array $jsonObject
* @return mixed $result
*/
private function processRequest( $jsonObject ) {


if (!isset($jsonObject["bean"])) {
echo "{'error':'nobean'}";
exit;
}
if (!isset($jsonObject["action"])) {
echo "{'error':'noaction'}";
exit;
}
if (!isset($jsonObject["data"])) {
$data = array();
}
else {
$data = $jsonObject["data"];
}

$beanType = strtolower(trim($jsonObject["bean"]));
$action = $jsonObject["action"];

try {
switch($action) {
case "store":

$data = $data[0];

if (!isset($data["id"])) $bean = R::dispense($beanType); else
$bean = R::load($beanType,$data["id"]);


$bean->import( $data );
$id = R::store($bean);
return $id;
break;
case "load":
$data = $data[0];
if (!isset($data["id"])) die("{'error':'noid'}");
$bean = R::load($beanType,$data["id"]);
return $bean->export();
break;
case "trash":
$data = $data[0];
if (!isset($data["id"])) die("{'error':'noid'}");
$bean = R::load($beanType,$data["id"]);
R::trash($bean);
return "OK";
break;
default:
$modelName = $this->modelHelper->getModelName( $beanType );
$beanModel = new $modelName;
return ( call_user_func_array(array($beanModel,$action), $data));
}
}
catch(Exception $exception) {
echo "{'exception':'".$exception->getCode()."-".$exception->getMessage()."'}";
exit;
}
}
}

11 changes: 10 additions & 1 deletion RedBean/ModelHelper.php
Expand Up @@ -19,7 +19,7 @@ class RedBean_ModelHelper implements RedBean_Observer {
* @param RedBean_OODBBean $bean
*/
public function onEvent( $eventName, $bean ) {
$className = "Model_".ucfirst( $bean->getMeta("type") );
$className = $this->getModelName( $bean->getMeta("type") );
if (class_exists($className)) {
$model = new $className;
if ($model instanceof RedBean_SimpleModel) {
Expand All @@ -31,4 +31,13 @@ public function onEvent( $eventName, $bean ) {
}
}

/**
* Returns the model associated with a certain bean.
* @param string $beanType
* @return string $modelClassName
*/
public function getModelName( $beanType ) {
return "Model_".ucfirst( $beanType );
}

}
1 change: 1 addition & 0 deletions RedBean/OODB.php
Expand Up @@ -234,6 +234,7 @@ public function store( RedBean_OODBBean $bean ) {
* @return RedBean_OODBBean $bean
*/
public function load($type, $id) {

$id = intval( $id );
if ($id < 0) throw new RedBean_Exception_Security("Id less than zero not allowed");
$bean = $this->dispense( $type );
Expand Down
29 changes: 29 additions & 0 deletions replica.php
@@ -0,0 +1,29 @@
<?php
$pat = "/\/\*([\n]|.)+?\*\//";



function clean($raw) {
global $pat;
$raw = str_replace("\r\n","\n",$raw); //str_replace("\r\n","\n", '<?php ' . "\n/** " . file_get_contents($base . 'license.txt') . "**/\n" . self::$class_definitions);
$raw = str_replace("\n\r","\n",$raw);
$raw = preg_replace("/\n+/", "\n", $raw);
$raw = str_replace("<?php", "", $raw);
$raw = str_replace("?>", "", $raw);
//$raw = preg_replace($pat, "", $raw);
$raw = preg_replace("/\/\/.*/", "", $raw);
return $raw;
}

$code = "<?php ";
$loader = simplexml_load_file("replica.xml");
$items = $loader->load->item;
foreach($items as $item) {
echo "Adding: $item \n";
$raw = file_get_contents( $item );
$code.=clean($raw);

}
//echo $code;
file_put_contents("rb.php", $code);

67 changes: 67 additions & 0 deletions replica.xml
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : replica.xml
Created on : 7 augustus 2010, 11:41
Author : Gabor de Mooij
Description:
-->

<root>
<load>
<item type="txt">RedBean/license.txt</item>
<item type="php">RedBean/ObjectDatabase.php</item>
<item type="php">RedBean/Plugin.php</item>

<item type="php">RedBean/Driver.php</item>
<item type="php">RedBean/Driver/PDO.php</item>

<item type="php">RedBean/OODBBean.php</item>
<item type="php">RedBean/Observable.php</item>
<item type="php">RedBean/Observer.php</item>

<item type="php">RedBean/Adapter.php</item>
<item type="php">RedBean/Adapter/DBAdapter.php</item>

<item type="php">RedBean/QueryWriter.php</item>
<item type="php">RedBean/QueryWriter/MySQL.php</item>
<item type="php">RedBean/QueryWriter/SQLite.php</item>
<item type="php">RedBean/QueryWriter/PostgreSQL.php</item>

<item type="php">RedBean/Exception.php</item>
<item type="php">RedBean/Exception/SQL.php</item>
<item type="php">RedBean/Exception/Security.php</item>
<item type="php">RedBean/Exception/FailedAccessBean.php</item>
<item type="php">RedBean/Exception/NotImplemented.php</item>
<item type="php">RedBean/Exception/UnsupportedDatabase.php</item>

<item type="php">RedBean/OODB.php</item>
<item type="php">RedBean/ToolBox.php</item>
<item type="php">RedBean/CompatManager.php</item>

<item type="php">RedBean/AssociationManager.php</item>
<item type="php">RedBean/TreeManager.php</item>
<item type="php">RedBean/LinkManager.php</item>
<item type="php">RedBean/ExtAssociationManager.php</item>
<item type="php">RedBean/Setup.php</item>
<item type="php">RedBean/SimpleStat.php</item>

<item type="php">RedBean/Plugin/ChangeLogger.php</item>
<item type="php">RedBean/Plugin/Cache.php</item>
<item type="php">RedBean/Plugin/Finder.php</item>
<item type="php">RedBean/Plugin/Constraint.php</item>

<item type="php">RedBean/DomainObject.php</item>
<item type="php">RedBean/Plugin/Optimizer.php</item>
<item type="php">RedBean/QueryWriter/NullWriter.php</item>
<item type="php">RedBean/UnitOfWork.php</item>
<item type="php">RedBean/SimpleModel.php</item>
<item type="php">RedBean/ModelHelper.php</item>
<item type="php">RedBean/Facade.php</item>

<item type="php">RedBean/BeanCan.php</item>


</load>
</root>

0 comments on commit 0cbb0e2

Please sign in to comment.