/** * Avos\Client::initialize, must be called before using Avos features. * * @param string $app_id Avos Application ID * @param string $app_key Avos REST API Key * @param string $master_key Avos Master Key * * @return null */ public static function initialize($app_id, $app_key, $master_key) { AVUser::registerSubclass(); AVRole::registerSubclass(); AVInstallation::registerSubclass(); self::$applicationId = $app_id; self::$appKey = $app_key; self::$masterKey = $master_key; if (!static::$storage) { if (session_status() === PHP_SESSION_ACTIVE) { self::setStorage(new AVSessionStorage()); } else { self::setStorage(new AVMemoryStorage()); } } }
/** * Save Object and unsaved children within. * * @param $target * @param bool $useMasterKey Whether to use the Master Key. * * @return null * * @throws AVException */ private static function deepSave($target, $useMasterKey = false) { $unsavedChildren = array(); $unsavedFiles = array(); static::findUnsavedChildren($target, $unsavedChildren, $unsavedFiles); $sessionToken = null; if (AVUser::getCurrentUser()) { $sessionToken = AVUser::getCurrentUser()->getSessionToken(); } foreach ($unsavedFiles as &$file) { $file->save(); } $objects = array(); // Get the set of unique objects among the children. foreach ($unsavedChildren as &$obj) { if (!in_array($obj, $objects, true)) { $objects[] = $obj; } } $remaining = $objects; while (count($remaining) > 0) { $batch = array(); $newRemaining = array(); foreach ($remaining as $key => &$object) { if (count($batch) > 40) { $newRemaining[] = $object; continue; } if ($object->canBeSerialized()) { $batch[] = $object; } else { $newRemaining[] = $object; } } $remaining = $newRemaining; if (count($batch) === 0) { throw new Exception("Tried to save a batch with a cycle."); } $requests = array(); foreach ($batch as $obj) { $json = $obj->getSaveJSON(); $method = 'POST'; $path = '/classes/' . $obj->getClassName(); if ($obj->getObjectId()) { $path .= '/' . $obj->getObjectId(); $method = 'PUT'; } $requests[] = array('method' => $method, 'path' => $path, 'body' => $json); } if (count($requests) === 1) { $req = $requests[0]; $result = AVClient::_request($req['method'], $req['path'], $sessionToken, json_encode($req['body']), $useMasterKey); $batch[0]->mergeAfterSave($result); } else { $result = AVClient::_request('POST', '/batch', $sessionToken, json_encode(array("requests" => $requests)), $useMasterKey); $errorCollection = array(); foreach ($batch as $key => &$obj) { if (isset($result[$key]['success'])) { $obj->mergeAfterSave($result[$key]['success']); } else { if (isset($result[$key]['error'])) { $response = $result[$key]; $error = $response['error']['error']; $code = isset($response['error']['code']) ? $response['error']['code'] : -1; $errorCollection[] = array('error' => $error, 'code' => $code, 'object' => $obj); } else { $errorCollection[] = array('error' => 'Unknown error in batch save.', 'code' => -1, 'object' => $obj); } } } if (count($errorCollection)) { throw new AVAggregateException("Errors during batch save.", $errorCollection); } } } }
/** * Get the defaultACL. * * @return AVACL * @ignore */ public static function _getDefaultACL() { if (self::$defaultACLUsesCurrentUser && self::$defaultACL) { $last = self::$lastCurrentUser ? clone self::$lastCurrentUser : null; if (!AVUser::getCurrentUser()) { return self::$defaultACL; } if ($last != AVUser::getCurrentUser()) { self::$defaultACLWithCurrentUser = clone self::$defaultAC; self::$defaultACLWithCurrentUser->_setShared(true); self::$defaultACLWithCurrentUser->setUserReadAccess(AVUser::getCurrentUser(), true); self::$defaultACLWithCurrentUser->setUserWriteAccess(AVUser::getCurrentUser(), true); self::$lastCurrentUser = clone AVUser::getCurrentUser(); } return self::$defaultACLWithCurrentUser; } return self::$defaultACL; }