{"id":19847159,"url":"https://github.com/ranaivomahaleo/tranoutilsbundle","last_synced_at":"2026-01-18T10:28:28.365Z","repository":{"id":57072427,"uuid":"242018293","full_name":"ranaivomahaleo/TranoUtilsBundle","owner":"ranaivomahaleo","description":"Add json httpstatus support, CORS headers support and .env file reader support to Symfony application.","archived":false,"fork":false,"pushed_at":"2024-06-12T08:10:44.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T18:05:27.655Z","etag":null,"topics":["cors","cors-request","environment-variables","httpstatus","jsonapi","php","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ranaivomahaleo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-21T00:12:08.000Z","updated_at":"2024-06-12T08:10:22.000Z","dependencies_parsed_at":"2022-08-24T14:54:35.331Z","dependency_job_id":null,"html_url":"https://github.com/ranaivomahaleo/TranoUtilsBundle","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ranaivomahaleo%2FTranoUtilsBundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ranaivomahaleo%2FTranoUtilsBundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ranaivomahaleo%2FTranoUtilsBundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ranaivomahaleo%2FTranoUtilsBundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ranaivomahaleo","download_url":"https://codeload.github.com/ranaivomahaleo/TranoUtilsBundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339132,"owners_count":20923009,"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":["cors","cors-request","environment-variables","httpstatus","jsonapi","php","symfony","symfony-bundle"],"created_at":"2024-11-12T13:13:32.805Z","updated_at":"2026-01-18T10:28:28.329Z","avatar_url":"https://github.com/ranaivomahaleo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TranoUtilsBundle  \n  \nThe TranoUtilsBundle contains utilities for the following purposes:\n- Extended Response with configurable headers using .env file variables\n- Json responses setter for REST API with configurable CORS headers using .env file variables\n- Simple syntax http query service\n- Environment variable reader\n\n## Installation  \n  \nInstall with composer using\n\n    composer require trano/tranoutilsbundle\n\n## Extended Response with configurable headers\n\nThe Response headers can be set using the following instruction:\n\n```php\nuse Trano\\UtilsBundle\\Util\\ExtendedResponse;\n\n$extendedreponse = new ExtendedResponse();\n$response = new Response('test');\n$responseWithHeaders = $extendedreponse-\u003eencapsulateHeaders($response);\n```\n\nThe headers values can be set using the following environment variables:\n\n```dotenv\nHEADER_STRICT_TRANSPORT_SECURITY=\"\"\nHEADER_CACHE_CONTROL=\"\"\nHEADER_PRAGMA=\"\"\nHEADER_REFERRER_POLICY=\"\"\nHEADER_X_CONTENT_TYPE_OPTIONS=\"\"\nHEADER_CONTENT_SECURITY_POLICY=\"\"\nHEADER_X_FRAME_OPTIONS=\"\"\nHEADER_X_XXS_PROTECTION=\"\"\n```\n\nBy default, the following secured header (https://owasp.org/) values are send back to the client when the corresponding environment variable is not set:\n\n```\nStrict-Transport-Security: max-age=31536000\nCache-Control: no-cache, no-store, must-revalidate\nPragma: no-cache\nReferrer-Policy: no-referrer\nX-Content-Type-Options: nosniff\nContent-Security-Policy: default-src 'self'\nX-Frame-Options: deny\nX-XXS-Protection: 1; mode=block\n```\n\nThe map between environment variables and the security headers are\n\n| Environment variable | Header | Default value |\n| --- | --- | --- |\n| HEADER_STRICT_TRANSPORT_SECURITY | Strict-Transport-Security | max-age=31536000 |\n| HEADER_CACHE_CONTROL | Cache-Control | no-cache, no-store, must-revalidate |\n| HEADER_PRAGMA | Pragma | no-cache |\n| HEADER_REFERRER_POLICY | Referrer-Policy | no-referrer |\n| HEADER_X_CONTENT_TYPE_OPTIONS | X-Content-Type-Options | nosniff |\n| HEADER_CONTENT_SECURITY_POLICY | Content-Security-Policy | default-src 'self' |\n| HEADER_X_FRAME_OPTIONS | X-Frame-Options | deny |\n| HEADER_X_XXS_PROTECTION | X-XXS-Protection | 1; mode=block |\n\n## JsonResponse with configurable CORS headers and security headers\n\n### JsonResponse with default security headers\n\nUse the following code to return a JsonResponse, code 200, with the default security headers:\n\n```php\nuse Trano\\UtilsBundle\\Util\\ApiJsonResponse;\n\n$apijsonreponse = new ApiJsonResponse();\nreturn $apijsonreponse-\u003e_200Ok('this is an ok results');\n```\n\nThe result of the above instructions is (notice the adition of the `status`, `message` and `results` keys)\n```json\n{\n  \"status\": 200,\n  \"message\": \"\",\n  \"results\": \"this is an ok results\"\n}\n```\n\nBy default, the following secured header values (https://owasp.org/)  are send back to the client:\n\n```\nStrict-Transport-Security: max-age=31536000\nCache-Control: no-cache, no-store, must-revalidate\nPragma: no-cache\nReferrer-Policy: no-referrer\nX-Content-Type-Options: nosniff\nContent-Security-Policy: default-src 'self'\nX-Frame-Options: deny\nX-XXS-Protection: 1; mode=block\n```\nThe above headers can be updated using the following environment variables\n```dotenv\nHEADER_STRICT_TRANSPORT_SECURITY=\"\"\nHEADER_CACHE_CONTROL=\"\"\nHEADER_PRAGMA=\"\"\nHEADER_REFERRER_POLICY=\"\"\nHEADER_X_CONTENT_TYPE_OPTIONS=\"\"\nHEADER_CONTENT_SECURITY_POLICY=\"\"\nHEADER_X_FRAME_OPTIONS=\"\"\nHEADER_X_XXS_PROTECTION=\"\"\n```\n\nTo update the CORS headers (```Access-Control.*```), use the following environment variables.\n\n```dotenv\nALLOWED_ORIGIN=\"*\"\nALLOWED_METHODS=\"GET,POST\"\nALLOWED_HEADERS=\"Authorization, Content-Type\"\n```\nThe map between environment variables and the headers are \n\n| Environment variable | Header |\n| --- | --- |\n| ALLOWED_ORIGIN | Access-Control-Allow-Origin |\n| ALLOWED_METHODS | Access-Control-Allow-Methods |\n| ALLOWED_HEADERS | Access-Control-Allow-Headers |\n\n### Custom JsonResponse\n\nBy default, `ApiJsonResponse` returns a json with `status`, `message` and `results` keys. To change this behaviour with a custom json data structure, set\n\n```dotenv\nJSON_RESPONSE_TYPE=custom\n```\n\nThus, the following php code\n\n```php\nuse Trano\\UtilsBundle\\Util\\ApiJsonResponse;\n\n$apijsonreponse = new ApiJsonResponse();\nreturn $apijsonreponse-\u003e_200Ok([\"data\" =\u003e 'this is an ok custom results']);\n```\nreturns\n\n```json\n{\n  \"data\": \"this is an ok custom results\"\n}\n```\n\n## Usage of simple Http request\n\nThe ```$httprequest``` service is an instance of ```Trano\\UtilsBundle\\Util\\HttpRequest```\n\n### GET query with basic Auth\n\nTo return an associative array response using GET, use the following instruction. The get instruction should be at the end.\n\n```php\n    $http_array_response = $this-\u003ehttprequest\n            -\u003eaddHeader('Accept', '*/*')\n            -\u003eaddHeader('Content-Type', 'application/json')\n            -\u003esetBasicAuth('username', 'password')\n            -\u003eget('https://www.example.com/getservice');\n```\n\n### POST query with array data and basic Auth\n\nTo return an associative array response using POST, use the instructions below.\n\nThe $data variable is sent by default using ```application/x-www-form-urlencoded``` type.\nIf ```https://www.example.com/postservice``` is a Symfony controller route, ```$data['data1']``` variable \nwould be get using ```$request-\u003erequest-\u003eget('data1');``` instruction.\n\nThe post instruction should be at the end.\n``\n```php\n    $data = [\n        'data1' =\u003e 'data1',\n        'data2' =\u003e 'data2',\n    ];\n    $http_array_response = $this-\u003ehttprequest\n        -\u003esetBasicAuth('username', 'password')\n        -\u003esetBodyArray($data)\n        -\u003epost('https://www.example.com/postservice');\n```\n\nNotice that due to the functionalities of the symfony http request, both GET and POST simple queries \nare synchronous (those queries wait for the response).\n\n## Usage of Environment variable reader\n\nLet us consider that we have the following environment variable file .env\n\n```\nDATABASE_URL='mysql://aaa:bbb...'\n```\n\nTo read this environment variable, use the $env service, an instance of ```Trano\\UtilsBundle\\Util\\Env``` \nas follows\n\n```php\n$database_url = $this-\u003eenv-\u003egetEnv('DATABASE_URL');\n````\n\n## Example with a Symfony controller  \n\nLet us consider the environment variable at .env file\n\n```\nALLOWED_ORIGIN=\"*\"\nALLOWED_METHODS=\"GET,POST,PUT,DELETE\"\nALLOWED_HEADERS=\"Authorization, Content-Type\"\n```\n\nImportant: If the ALLOWED_* are not set in .env file, the corresponsing \nAccess-Control-Allow-* headers will not be set.\n\nTo use the environment reader (```Trano\\UtilsBundle\\Util\\Env```) \nand the Json response (```Trano\\UtilsBundle\\Util\\ApiJsonResponse```) in a controller, use the following script\n\n```php\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nuse Symfony\\Component\\Routing\\Annotation\\Route;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Trano\\UtilsBundle\\Util\\ApiJsonResponse;\nuse Trano\\UtilsBundle\\Util\\Env;\n\nclass HomeController extends AbstractController\n{\n    /**\n     * @var ApiJsonResponse\n     */\n    private $apijsonresponse;\n\n\n    /**\n     * @var Env\n     */\n    private $env;\n\n    /**\n     * ControllerTrait constructor.\n     * @param ApiJsonResponse $apijsonresponse\n     * @param Env $env\n     */\n    public function __construct(ApiJsonResponse $apijsonresponse, Env $env)\n    {\n        $this-\u003eapijsonresponse = $apijsonresponse;\n        $this-\u003eenv = $env;\n    }\n\n    /**\n     * @Route(\"/\", methods={\"GET\"})\n     */\n    public function index(Request $request)\n    {\n        // Computes DATABASE_URL from .env file\n        // If DATABASE_URL does not exists in .env file, it returns '' string.\n        $database_url = $this-\u003eenv-\u003egetEnv('DATABASE_URL');\n    \n        // Return Json response with the http status 200.\n        return $this-\u003eapijsonresponse-\u003e_200Ok('this is an ok results');\n    } // index\n}\n```\nThe route / above reads DATABASE_URL variable environment and returns the following json data.\n\n    {\n        \"status\": 200, \n        \"message\": \"\",\n        \"results\": \"this is an ok results\"\n    }\n  \nLicense  \n-------  \nThis bundle is under the MIT license. See the complete license [in the bundle](LICENSE).  \n  \nAbout us  \n--------  \nTranoUtilsBundle is an initiative of [atety][1].  \n  \n[1]: https://www.atety.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Franaivomahaleo%2Ftranoutilsbundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Franaivomahaleo%2Ftranoutilsbundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Franaivomahaleo%2Ftranoutilsbundle/lists"}