Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabor de Mooij, Buurtnerd
committed
Aug 28, 2010
1 parent
ad546fe
commit 0cbb0e2
Showing
5 changed files
with
270 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
|
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,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); | ||
|
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,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> |