1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363:
<?php
namespace LeanCloud\Engine;
use LeanCloud\Client;
/**
* Cloud functions and hooks repository
*
*/
class Cloud {
/**
* Map of function (or hook) name to callable functions
*
* @var array
*/
private static $repo = array();
/**
* Hook map
*/
private static $hookMap = array(
"beforeSave" => "__before_save_for_",
"afterSave" => "__after_save_for_",
"beforeUpdate" => "__before_update_for_",
"afterUpdate" => "__after_update_for_",
"beforeDelete" => "__before_delete_for_",
"afterDelete" => "__after_delete_for_",
"onLogin" => "__on_login_",
"onVerified" => "__on_verified_",
"onComplete" => "__on_complete_"
);
public static function getKeys() {
return array_keys(self::$repo);
}
/**
* Get defined function or hook by internal name
*
* @param string $funcName Name of function or hook
* @return callable|null
*/
private static function getFunc($funcName) {
return (isset(self::$repo[$funcName]) ? self::$repo[$funcName] : null);
}
/**
* Get internal hook name
*
* @param string $hookName
* @return string
*/
private static function getHookPrefix($hookName) {
return (isset(self::$hookMap[$hookName]) ?
self::$hookMap[$hookName] : null);
}
/**
* Define a cloud function
*
* The function shall take two arguments: the first is an array of
* parameters, the second is user in the session. Example:
*
* ```php
* Cloud::define("sayHello", function($params, $user) {
* return "Hello {$params['name']}!";
* });
* ```
*
* @param string $funcName
* @param callable $func
* @see self::run
*/
public static function define($funcName, $func) {
self::$repo[$funcName] = $func;
}
/**
* Define before save hook for a class
*
* The function shall take two arguments: the first one is class
* object, the second is user if available in session. If your $func
* throws `FunctionError`, the save will be rejected. Example:
*
* ```php
* Cloud::beforeSave("TestObject", function($object, $user) {
* $title = $object->get("title");
* if (strlen($title) > 140) {
* // Throw error and reject the save operation.
* throw new FunctionError("Title is too long", 1);
* }
* // else object will be saved.
* });
* ```
*
* @param string $className
* @param callable $func
* @see FunctionError
*/
public static function beforeSave($className, $func) {
$name = self::getHookPrefix("beforeSave") . $className;
self::define($name, $func);
}
/**
* Define after save hook for a class
*
* The function shall take two arguments: the first one is class
* object, the second is user if available in session. Any error
* in after hook will be ignored.
*
* @param string $className
* @param callable $func
* @see FunctionError
*/
public static function afterSave($className, $func) {
$name = self::getHookPrefix("afterSave") . $className;
self::define($name, $func);
}
/**
* Define before update hook for a class
*
* The function shall take two arguments: the first one is class
* object, the second is user if available in session. If your $func
* throws `FunctionError`, the update will be rejected.
*
* @param string $className
* @param callable $func
* @see FunctionError
*/
public static function beforeUpdate($className, $func) {
$name = self::getHookPrefix("beforeUpdate") . $className;
self::define($name, $func);
}
/**
* Define after update hook for a class
*
* The function shall take two arguments: the first one is class
* object, the second is user if available in session. Any error
* in $func will be ignored.
*
* @param string $className
* @param callable $func
* @see FunctionError
*/
public static function afterUpdate($className, $func) {
$name = self::getHookPrefix("afterUpdate") . $className;
self::define($name, $func);
}
/**
* Define before delete hook for a class
*
* The function shall take two arguments: the first one is class
* object, the second is user if available in session. If your $func
* throws `FunctionError`, the delete will be rejected.
*
* @param string $className
* @param callable $func
* @see FunctionError
*/
public static function beforeDelete($className, $func) {
$name = self::getHookPrefix("beforeDelete") . $className;
self::define($name, $func);
}
/**
* Define after delete hook for a class
*
* The function shall take two arguments: the first one is class
* object, the second is user if available in session. Any error
* in $func will be ignored.
*
* @param string $className
* @param callable $func
* @see FunctionError
*/
public static function afterDelete($className, $func) {
$name = self::getHookPrefix("afterDelete") . $className;
self::define($name, $func);
}
/**
* Define hook for when user tries to login
*
* The function takes one argument, the login user. A `FunctionError`
* could be thrown in the $func, which will reject the user for login.
*
* @param callable $func
* @see self::runOnLogin
*/
public static function onLogin($func) {
self::define("__on_login__User", $func);
}
/**
* Define hook for when user verified sms or email
*
* The function takes one argument, the verified user.
*
* @param string $type Either "sms" or "email"
* @param callable $func
* @see self::runOnVerified
*/
public static function onVerified($type, $func) {
self::define("__on_verified_{$type}", $func);
}
/**
* Define on complete hook for big query
*
* The function takes one argument, the big query job info as array:
*
* ```php
* array(
* "id" => "job id",
* "status" => "OK/ERROR",
* "message" => "..."
* );
* ```
*
* @param callable $func
* @see self::runOnInsight
*/
public static function onInsight($func) {
self::define("__on_complete_bigquery_job", $func);
}
/**
* Run cloud function
*
* Example:
*
* ```php
* LeanEngine::run("sayHello", array("name" => "alice"), $user);
* // sayHello(array("name" => "alice"), $user);
* ```
*
* @param string $funcName Name of defined function
* @param array $data Array of parameters passed to function
* @param User $user Request user
* @param array $meta Optional parameters that will be passed to
* user function
* @return mixed
* @throws FunctionError
* @see self::define
*/
public static function run($funcName, $params, $user=null, $meta=array()) {
$func = self::getFunc($funcName);
if (!$func) {
throw new FunctionError("Cloud function not found.", 404);
}
return call_user_func($func, $params, $user, $meta);
}
/**
* Start cloud function Stand-alone mode, start to process request.
*/
public static function start() {
Client::initialize(
getenv("LEANCLOUD_APP_ID"),
getenv("LEANCLOUD_APP_KEY"),
getenv("LEANCLOUD_APP_MASTER_KEY")
);
$engine = new LeanEngine();
$engine->start();
}
public static function stop() {
}
/**
* Run cloud hook
*
* Example:
*
* ```php
* LeanEngine::runHook("TestObject", "beforeUpdate", $object, $user);
* // hook($object, $user);
* ```
*
* @param string $className Classname
* @param string $hookName Hook name, e.g. beforeUpdate
* @param LeanObject $object The object of attached hook
* @param User $user Request user
* @param array $meta Optional parameters that will be passed to
* user function
* @return mixed
* @throws FunctionError
*/
public static function runHook($className, $hookName, $object,
$user=null,
$meta=array()) {
$name = self::getHookPrefix($hookName) . $className;
$func = self::getFunc($name);
if (!$func) {
throw new FunctionError("Cloud hook `{$name}' not found.",
404);
}
return call_user_func($func, $object, $user, $meta);
}
/**
* Run hook when a user logs in
*
* @param User $user The user object that tries to login
* @param array $meta Optional parameters that will be passed to
* user function
* @throws FunctionError
* @see self::onLogin
*/
public static function runOnLogin($user, $meta=array()) {
return self::runHook("_User", "onLogin", $user, $meta);
}
/**
* Run hook when user verified by Email or SMS
*
* @param string $type Either "sms" or "email", case-sensitive
* @param User $user The verifying user
* @param array $meta Optional parameters that will be passed to
* user function
* @throws FunctionError
* @see self::onVerified
*/
public static function runOnVerified($type, $user, $meta=array()) {
$name = "__on_verified_{$type}";
$func = self::getFunc($name);
if (!$func) {
throw new FunctionError("Cloud hook `{$name}' not found.",
404);
}
return call_user_func($func, $user, $meta);
}
/**
* Run hook on big query complete
*
* @param array $params Big query job info
* @param array $meta Optional parameters that will be passed to
* user function
* @return mixed
* @throws FunctionError
* @see self::onInsight
*/
public static function runOnInsight($params, $meta=array()) {
$name = "__on_complete_bigquery_job";
$func = self::getFunc($name);
if (!$func) {
throw new FunctionError("Cloud hook `{$name}' not found.",
404);
}
return call_user_func($func, $params, $meta);
}
}