From 9d72a47f85c4a4fb3186c91d7ae59487e14e5933 Mon Sep 17 00:00:00 2001 From: Plokko Date: Sun, 26 Nov 2023 13:31:40 +0100 Subject: [PATCH] Added database URL option, added RealtimeDb.rules --- src/IO/Database.php | 77 ++++++++++++++++++++++------------ src/IO/DatabaseRules.php | 89 ++++++++++++++++++++++++++++++++++++++++ src/IO/Reference.php | 14 +++---- 3 files changed, 147 insertions(+), 33 deletions(-) create mode 100644 src/IO/DatabaseRules.php diff --git a/src/IO/Database.php b/src/IO/Database.php index 20a3305..777d43f 100644 --- a/src/IO/Database.php +++ b/src/IO/Database.php @@ -4,7 +4,8 @@ use GuzzleHttp\Client; use Plokko\Firebase\ServiceAccount; -class Database{ +class Database +{ /**@var \Plokko\Firebase\ServiceAccount **/ private $serviceAccount; /**@var string **/ @@ -12,30 +13,34 @@ class Database{ /**@var \GuzzleHttp\ClientInterface **/ private $client; - private $debug=false; + private $debug = false; /** * @param ServiceAccount $serviceAccount Service account * @param string $databaseUrl specify database URL, if not set https://.firebaseio.com/ will be used */ - function __construct(ServiceAccount $serviceAccount, $databaseUrl=null) + function __construct(ServiceAccount $serviceAccount, $databaseUrl = null) { $this->serviceAccount = $serviceAccount; - $this->baseUrl = $databaseUrl?: 'https://'.$serviceAccount->getProjectId().'.firebaseio.com/'; + $this->baseUrl = $databaseUrl !== null ? $databaseUrl . '/' : ('https://' . $serviceAccount->getProjectId() . '.firebaseio.com/'); } /** - * @param $path + * Return the reference of a path + * @param string $path * @return Reference */ - function getReference($path){ - return new Reference($this,$path); + function getReference($path) + { + return new Reference($this, $path); } - function __get($path){ + function __get($path) + { return $this->getReference($path); } - function __set($path,$value){ + function __set($path, $value) + { } @@ -43,37 +48,57 @@ function __set($path,$value){ * Get the http client instance * @return \GuzzleHttp\ClientInterface */ - private function getClient(){ - if(!$this->client){ - $this->client = $this->serviceAccount->authorize(['https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/firebase.database'], new Client(['debug'=>$this->debug])); + private function getClient(): \GuzzleHttp\ClientInterface + { + if (!$this->client) { + $this->client = $this->serviceAccount->authorize(['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/firebase.database'], new Client(['debug' => $this->debug])); } return $this->client; } - private function getUriPath($path){ - return $this->baseUrl.trim($path,'/').'.json'; + private function getUriPath($path) + { + return $this->baseUrl . trim($path, '/') . '.json'; } - function get($path){ - $response = $this->getClient()->request('GET',$this->getUriPath($path)); - return json_decode($response->getBody(),true); + function get($path) + { + $response = $this->getClient()->request('GET', $this->getUriPath($path)); + return json_decode($response->getBody(), true); } - function set($path,$value){ - $this->getClient()->request('PUT',$this->getUriPath($path),['json'=>$value]); + function set($path, $value) + { + $this->getClient()->request('PUT', $this->getUriPath($path), ['json' => $value]); } - function update($path,$value){ - $this->getClient()->request('PATCH',$this->getUriPath($path),['json'=>$value]); + function update($path, $value) + { + $this->getClient()->request('PATCH', $this->getUriPath($path), ['json' => $value]); } - function delete($path){ - $this->getClient()->request('DELETE',$this->getUriPath($path),[]); + function delete($path) + { + $this->getClient()->request('DELETE', $this->getUriPath($path), []); } - function setDebug($debug=false){ - $this->debug=$debug; - $this->client=null; + function setDebug($debug = false) + { + $this->debug = $debug; + $this->client = null; + return $this; + } + + function getRules(): DatabaseRules + { + $response = $this->getClient()->request('GET', $this->getUriPath('.settings/rules')); + $data = json_decode($response->getBody(), true); + return new DatabaseRules($this, $data); + } + function setRules(array $rules): void + { + $this->getClient()->request('PUT', $this->getUriPath('.settings/rules'), ['body' => json_encode($rules, JSON_PRETTY_PRINT)]); } + } diff --git a/src/IO/DatabaseRules.php b/src/IO/DatabaseRules.php new file mode 100644 index 0000000..a47c6a3 --- /dev/null +++ b/src/IO/DatabaseRules.php @@ -0,0 +1,89 @@ +database = $database; + $this->rules = $rules; + } + + /** + * Save current rules to Firebase + */ + public function saveRules(): void + { + $this->database->setRules($this->toArray()); + } + + /** + * Replace rules with new ones + * @param array $newRules New rules to apply + */ + public function setRules(array $newRules): void + { + $this->rules = $newRules; + } + + /** + * Get current rules as an Array + * @return array + */ + function getRules(): array + { + return $this->rules; + } + + /** + * Get current rules as an Array + * @return array + */ + function toArray(): array + { + return $this->rules; + } + + function jsonSerialize(): mixed + { + return $this->rules; + } + + + function &__get($path) + { + return $this->rules[$path]; + } + + function __set($path, $value) + { + $this->rules[$path] = $value; + } + + function offsetExists($offset): bool + { + return isset($this->rules[$offset]); + } + + function offsetGet($offset): mixed + { + return $this->rules[$offset]; + } + + function offsetSet($offset, $value): void + { + $this->rules[$offset] = $value; + } + function offsetUnset($offset): void + { + unset($this->rules[$offset]); + } +} diff --git a/src/IO/Reference.php b/src/IO/Reference.php index 9f5e8d0..7c2794a 100644 --- a/src/IO/Reference.php +++ b/src/IO/Reference.php @@ -7,10 +7,10 @@ class Reference implements JsonSerializable { private - $db, - $path, + $db, + $path, - $data = null; + $data = null; function __construct(Database $db, $path) { @@ -23,7 +23,7 @@ function __construct(Database $db, $path) * @param $path string optional sub path to concatenate * @return string */ - function getPath($path = '') + function getPath($path = ''): string { return $this->path . '/' . trim($path, '/'); } @@ -45,7 +45,7 @@ public function fetch() * @param $path * @return Reference */ - function getReference($path) + function getReference($path): Reference { return new Reference($this->db, $this->path . '/' . $path); } @@ -65,7 +65,7 @@ function update($path, $value) $this->db->update($this->getPath($path), $value); } - function delete($path = '') + function delete($path = ''): void { $this->db->delete($this->getPath($path)); } @@ -83,7 +83,7 @@ function __set($path, $value) } - function toArray() + function toArray(): array { return $this->getData(); }