statamic / v2-hub

Statamic 2 - Feature Requests and Bug Reports
https://statamic.com
95 stars 5 forks source link

Storage API and ContextualStorage is missing a lot of helpers #2369

Closed FrittenKeeZ closed 5 years ago

FrittenKeeZ commented 5 years ago

Describe the bug Calling $this->storage->exists($key) or $this->storage->delete($key) from an addon fails as the methods don't exists.

Expected behavior Equivalent helpers of putJSON for exists and delete should work.

Environment details (please complete the following information):

Additional context Patch to fix everything:

diff --git statamic/core/API/Storage.php statamic/core/API/Storage.php
index 23f21a8..f547593 100644
--- statamic/core/API/Storage.php
+++ statamic/core/API/Storage.php
@@ -8,7 +8,7 @@ class Storage
      * Save a key to storage
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public static function put($key, $data)
     {
@@ -19,7 +19,7 @@ class Storage
      * Save a key to storage as YAML
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public static function putYAML($key, $data)
     {
@@ -34,7 +34,7 @@ class Storage
      * Save a key to storage as a serialized array
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public static function putSerialized($key, $data)
     {
@@ -45,7 +45,7 @@ class Storage
      * Save a key to storage as JSON
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public static function putJSON($key, $data)
     {
@@ -53,7 +53,7 @@ class Storage
     }

     /**
-     * Check if a key exists
+     * Check if a key exists in storage
      *
      * @param  string $key Key to check
      * @return bool
@@ -63,6 +63,39 @@ class Storage
         return File::disk('storage')->exists(self::getPath($key));
     }

+    /**
+     * Check if a key exists in storage as YAML
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public static function existsYAML($key)
+    {
+        return self::exists(Str::ensureRight($key, '.yaml'));
+    }
+
+    /**
+     * Check if a key exists in storage as a serialized array
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public static function existsSerialized($key)
+    {
+        return self::exists(Str::ensureRight($key, '.php'));
+    }
+
+    /**
+     * Check if a key exists in storage as JSON
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public static function existsJSON($key)
+    {
+        return self::exists(Str::ensureRight($key, '.json'));
+    }
+
     /**
      * Delete a key from storage
      *
@@ -74,7 +107,37 @@ class Storage
     }

     /**
-     * Get a key from the cache
+     * Delete a YAML key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public static function deleteYAML($key)
+    {
+        self::delete(Str::ensureRight($key, '.yaml'));
+    }
+
+    /**
+     * Delete a serialized array key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public static function deleteSerialized($key)
+    {
+        self::delete(Str::ensureRight($key, '.php'));
+    }
+
+    /**
+     * Delete a JSON key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public static function deleteJSON($key)
+    {
+        self::delete(Str::ensureRight($key, '.json'));
+    }
+
+    /**
+     * Get a key from the storage
      *
      * @param string $key      Key to retrieve
      * @param null   $default  Fallback data if the value doesn't exist
diff --git statamic/core/Extend/Contextual/ContextualStorage.php statamic/core/Extend/Contextual/ContextualStorage.php
index c0ab8ad..17d2f01 100644
--- statamic/core/Extend/Contextual/ContextualStorage.php
+++ statamic/core/Extend/Contextual/ContextualStorage.php
@@ -10,7 +10,7 @@ class ContextualStorage extends ContextualObject
      * Save a key to storage
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public function put($key, $data)
     {
@@ -21,7 +21,7 @@ class ContextualStorage extends ContextualObject
      * Save a key to storage as YAML
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public function putYAML($key, $data)
     {
@@ -32,7 +32,7 @@ class ContextualStorage extends ContextualObject
      * Save a key to storage as a serialized array
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public function putSerialized($key, $data)
     {
@@ -43,7 +43,7 @@ class ContextualStorage extends ContextualObject
      * Save a key to storage as JSON
      *
      * @param string $key   Key to save under
-     * @param mixed  $data  Data to cache
+     * @param mixed  $data  Data to store
      */
     public function putJSON($key, $data)
     {
@@ -51,7 +51,91 @@ class ContextualStorage extends ContextualObject
     }

     /**
-     * Get a key from the cache
+     * Check if a key exists in the storage
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public function exists($key)
+    {
+        return Storage::exists($this->contextualize($key));
+    }
+
+    /**
+     * Check if a key exists in storage as YAML
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public function existsYAML($key)
+    {
+        return Storage::existsYAML($this->contextualize($key));
+    }
+
+    /**
+     * Check if a key exists in storage as a serialized array
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public function existsSerialized($key)
+    {
+        return Storage::existsSerialized($this->contextualize($key));
+    }
+
+    /**
+     * Check if a key exists in storage as JSON
+     *
+     * @param  string $key Key to check
+     * @return bool
+     */
+    public function existsJSON($key)
+    {
+        return Storage::existsJSON($this->contextualize($key));
+    }
+
+    /**
+     * Delete a key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public function delete($key)
+    {
+        Storage::delete($this->contextualize($key));
+    }
+
+    /**
+     * Delete a YAML key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public function deleteYAML($key)
+    {
+        Storage::deleteYAML($this->contextualize($key));
+    }
+
+    /**
+     * Delete a serialized array key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public function deleteSerialized($key)
+    {
+        Storage::deleteSerialized($this->contextualize($key));
+    }
+
+    /**
+     * Delete a JSON key from storage
+     *
+     * @param string $key   Key to delete
+     */
+    public function deleteJSON($key)
+    {
+        Storage::deleteJSON($this->contextualize($key));
+    }
+
+    /**
+     * Get a key from the storage
      *
      * @param string $key      Key to retrieve
      * @param null   $default  Fallback data if the value doesn't exist
alexanderhofstaetter commented 5 years ago

This is still not fixed. I have the same problem because i wanted to use the exists method.

jesseleite commented 5 years ago

Will be in next release. Thanks for the implementation @FrittenKeeZ