{"id":16721107,"url":"https://github.com/khalyomede/fetch","last_synced_at":"2025-03-15T12:23:17.550Z","repository":{"id":57006177,"uuid":"123344640","full_name":"khalyomede/fetch","owner":"khalyomede","description":"Quickly retrieve your PHP data","archived":false,"fork":false,"pushed_at":"2018-03-07T23:29:37.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T03:47:37.751Z","etag":null,"topics":["config","configuration","data","fetch","php","php7"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/khalyomede/fetch","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khalyomede.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-28T21:29:24.000Z","updated_at":"2022-02-12T09:18:48.000Z","dependencies_parsed_at":"2022-08-21T14:30:31.424Z","dependency_job_id":null,"html_url":"https://github.com/khalyomede/fetch","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Ffetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Ffetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Ffetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Ffetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khalyomede","download_url":"https://codeload.github.com/khalyomede/fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243727249,"owners_count":20337956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","configuration","data","fetch","php","php7"],"created_at":"2024-10-12T22:28:48.702Z","updated_at":"2025-03-15T12:23:17.525Z","avatar_url":"https://github.com/khalyomede.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fetch\n\n![PHP from Packagist](https://img.shields.io/packagist/php-v/khalyomede/fetch.svg)\n![Packagist](https://img.shields.io/packagist/v/khalyomede/fetch.svg)\n![Packagist](https://img.shields.io/packagist/l/khalyomede/fetch.svg)\n\nQuickly retrieve your PHP data.\n\nFrom\n\n```\n/config\n  /database\n    database.php\n    option.php\n  app.php\n```\n\nTo\n\n```php\n$fetch = new Fetch('config');\n\n$command = $fetch-\u003efrom('database.option.initial-command');\n```\n\n## Summary\n\n- [Prerequistes](#prerequisites)\n- [Installation](#installation)\n- [Examples of uses](#examples-of-uses)\n- [Prototype ready](#prototype-ready)\n- [Methods definitions](#methods-definitions)\n- [MIT licence](#mit-licence)\n\n## Prerequisites\n\n- PHP version \u003e= 7.0.0\n\n## Installation\n\nIn your project folder:\n\n```bash\ncomposer require khalyomede/fetch:1.*\n```\n\n## Examples of uses\n\nAll the examples below assume we are on the root (inside the `index.php` file) and we have the following arborescence:\n\n```\n/config\n  /database\n    database.php\n    option.php\n  app.php\nindex.php\n```\n\n- [Example 1: fetching a data from a simple file](#example-1-fetching-a-data-from-a-single-file)\n- [Example 2: fetching a data by traversing multiple folders](#example-2-fetching-a-data-by-traversing-multiple-folders)\n- [Example 3: fetching a data inside nested keys](#example-3-fetching-a-data-inside-nested-keys)\n- [Example 4: fetching some data using a specific key](#example-4-fetching-some-data-using-a-specific-key)\n- [Example 5: fetching faster using cache](#example-5-fetching-faster-using-cache)\n- [Example 6: using a function before actually fetching the data](#example-6-using-a-function-before-actually-fetching-the-data)\n\n### Example 1: fetching a data from a simple file\n\nAll the configuration file should return a PHP array.\n\nLet us assume the file `app.php` contains:\n\n```php\nreturn [\n  'name' =\u003e 'My app',\n  'charset' =\u003e 'utf-8'\n];\n```\nWe can fetch the charset using:\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$charset = $fetch-\u003efrom('app.charset');\n\nprint_r($charset);\n```\n\nThis will display\n\n```\nutf-8\n```\n\n### Example 2: fetching a data by traversing multiple folders\n\nAll the configuration files should return a PHP array.\n\nLet us assume the file `option.php` contains:\n\n```php\nreturn [\n  'initial-command' =\u003e \"SET names 'utf-8'\"\n];\n```\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$initial_command = $fetch-\u003efrom('database.option.initial-command');\n\nprint_r($initial_command);\n```\n\nWill display\n\n```\nSET names 'utf-8'\n```\n\n### Example 3: fetching a data inside nested keys\n\nAll configuration files should return a PHP array.\n\nLet us assume the file `option.php` contains:\n\n```php\nreturn [\n  'cache' =\u003e [\n    'strategy' =\u003e 'cache-first'\n  ]\n];\n```\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$strategy = $fetch-\u003efrom('database.option.cache.strategy');\n\nprint_r($stategy);\n```\n\nWill display:\n\n```\ncache-first\n```\n\n### Example 4: fetching some data using a specific key\n\nAll configuration files should return PHP array.\n\nLet us assume the file `option.php` contains:\n\n```php\nreturn [\n  'cache' =\u003e [\n    'strategy' =\u003e 'cache-first'\n  ],\n  'timeout' =\u003e 12.5\n];\n```\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$data = $fetch-\u003efrom('database.option');\n\nprint_r($data);\n```\n\nWill display:\n\n```php\nArray\n(\n  [cache] =\u003e Array\n    (\n      [strategy] =\u003e cache-first\n    )\n  [timeout] =\u003e 12.5\n)\n```\n\n### Example 5: fetching faster using cache\n\nLet us assume a new folder has been created, called `cache`. Here is the updated arborescence:\n\n```\n/config\n  /database\n    option.php\n  app.php\n/cache\nindex.php\n```\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$fetch-\u003eusingCache( __DIR__ . '/cache' );\n\n$charset = $fetch-\u003efrom('app.charset');\n```\n\nThe first fetching might be slow, but the subsequent fetches will be much more faster while the cached data is not removed.\n\n**Note**\n\nThis library will not handles the cache removing.\n\n**Tips**\n\nThink to disable the cache when you are in development mode as your configuration file will changed a lot and might be outdated without updating to the new value. This tips features Laravel 5 debug helper.\n\n```php\nuse Khalyomede\\Fetch;\n\n// ...\n\n$fetch = new Fetch( base_path() . '/config' );\n\nif( config('app.debug') !== true ) {\n  $fetch-\u003eusingCache( base_path() . '/' )\n}\n\n$language = $fetch-\u003efrom('app.locale');\n```\n\n**Warning**\n\nThe cache might be outdated if you use it without clearing it after an update of your configuration data.\n\n### Example 6: using a function before actually fetching the data\n\nYou can apply \"middleware\" function before fetching/caching the data if needed.\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$remove_dashes = function ($data) {\n  return str_replace('-', '', $data);\n};\n\n$charset = $fetch-\u003eacross($remove_dashes)-\u003efrom('app.charset');\n\nprint_r($charset);\n```\n\nWill display\n\n```\nutf8\n```\n\n**Note**\n\nThe anonymous takes only one parameter and it can be frustrating. Use `use` statement to help you pass more arguments and overcome this constraint:\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$now = (new DateTime())-\u003eformat('Y-m-d');\n\n$charset = $fetch-\u003eacross(function($data) use($now) {\n  return \"$data ($now)\";\n})-\u003efrom('app.charset');\n\nprint_r($charset);\n```\n\nWill display\n\n```\nutf-8 (2018-03-03)\n```\n\n**Note 2**\n\nOnce the function is set, it is applyied for any further fetches. If you want to remove the function, you can use `uncross()` to do so:\n\n```php\nuse Khalyomede\\Fetch;\n\n$fetch = new Fetch( __DIR__ . '/config' );\n\n$fetch-\u003eacross(function($data) {\n  return str_replace('-', '', $data);\n});\n\n$charset = $fetch-\u003efrom('app.charset'); \n\nprint_r($charset);\n\n$fetch-\u003euncross(); // or $fetch-\u003euncross()-\u003efrom('...');\n\n$timeout = $fetch-\u003efrom('database.option.timeout');\n\nprint_r($timeout);\n```\n\nWill display\n\n```\nutf8\n12.5\n```\n\n## Prototype ready\n\nThis class lets you extend its functionality to your needs without having to dive into the source code. For example:\n\n```php\n$fetch-\u003eprototype('exists', function($path) {\n  $exists = false;\n\n  try {\n    $this-\u003efrom($path);\n\n    $exists = true;\n  }\n  catch(Exception $e) {}\n\n  return $exists;\n});\n\nif($fetch-\u003eexists('test')) {\n  echo $fetch-\u003efrom('test');\n}\nelse {\n  echo 'this configuration does not exists';\n}\n```\n\nFor more information, check [khalyomede/prototype](https://github.com/khalyomede/prototype) documentation.\n\n## Methods definitions\n\n- [across()](#across)\n- [construct()](#construct)\n- [crypt()](#crypt)\n- [decrypt()](#decrypt)\n- [disableCache()](#disablecache)\n- [enableCache()](#enablecache)\n- [from()](#from)\n- [uncross()](#uncross)\n- [usingCache()](#usingcache)\n\n### construct()\n\nSets the default folder to fetch the data from.\n\n```php\npublic function __construct(string $folder_path): Fetch\n```\n\n**Exceptions**\n\n`InvalidArgumentException`:\n\n- If the folder path does not exists\n- If the folder path is not a folder\n\n### from()\n\nFetch the data from the given path.\n\n```php\npublic function from(string $path): mixed\n```\n\n**Exceptions**\n\n`InvalidArgumentException`:\n\n- If the path is empty\n- If one of the key is empty (for example: \"database.option.\")\n- If the targeted configuration file does not return an array\n- If the path targets an non existing key\n\n`UnexpectedValueException`:\n\n- If the cache folder has not been specifyied when the cache mode has been enabled\n\n`RuntimeException`:\n\n- If the targeted configuration file is not available is read mode\n- If the cached data file is not available in read mode\n\n### usingCache()\n\nSpecify the folder for storing the cached data and enable the cache mode.\n\n```php\npublic function usingCache(string $cache_folder_path): Fetch\n```\n\n**Exceptions**\n\n`InvalidArgumentException`:\n\n- If the folder does not exists\n- If the path does not target a folder\n\n### enableCache()\n\nEnable the cache mode.\n\n```php\npublic function enableCache(): Fetch\n```\n\n### disableCache()\n\nDisable the cache mode.\n\n```php\npublic function disableCache(): Fetch\n```\n\n### encrypt()\n\nUseful when you need to get the same files name to perform additional processes from a path.\n\n```php\npublic function encrypt(string $string): string\n```\n\n### decrypt()\n\nUseful when you want to get the original path used for storing the cached file using the cached file name.\n\n```php\npublic function decrypt(string $string): string\n```\n\n**Note**\n\nDo not include the file extension (`.php`) when decrypting using the file name.\n\n### across()\n\nApply a function (which should be an anonymous, i.e. a closure) before fetching or caching the data.\nThe function should have only one parameter, which will be filled with the fetched data.\n\n```php\npublic function across(callable $function): Fetch\n```\n\n**Exceptions**\n\n`InvalidArgumentException`:\n- If the function is not an anonymous function (i.e. a closure)\n- If the function does not have exactly one parameter\n- If the function does not have exactly one required parameter\n\n`ReflectionException`:\n- Check the [package website](http://php.net/manual/en/reflectionfunction.construct.php#refsect1-reflectionfunction.construct-errors) for more information\n\n### uncross()\n\nRemoves the previously set function (made by `Fetch::across`).\n\n```php\npublic function uncross(): Fetch\n```\n\n## MIT Licence\n\nFetch\n\nCopyright © 2018 Khalyomede\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the oftware, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN CTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Ffetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhalyomede%2Ffetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Ffetch/lists"}