2017 © Pedro Peláez
 

library flex-php-io

IO library for web and CLI applications.

image

tbolner/flex-php-io

IO library for web and CLI applications.

  • Saturday, May 5, 2018
  • by tbolner
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

FlexPhpIO

An example of an I/O library. This project shows an alternative to handling output through the router framework. Most router frameworks expect the output to be passed back in the controllers in a "return" statement. Then for example they would convert the array to JSON., (*1)

In my opinion that solution becomes too problematic, when changes and new output modes are introduced later on to the project. Routing and IO are better be separated into 2 distinct libraries., (*2)

Functions:, (*3)

  • Help handling output buffering
  • Handy methods to change content type
  • Access HTTP/GET parameters in a type-safe manner.
  • Help with the JSON format (input / output)
Request Response
Request::getPostedJson(): array Response::printJson(array $json, $http_status_code = 200)
Request::getIntParameter(string $paramName): int Response::bufferStart()
Request::getFloatParameter(string $paramName): float Response::bufferEndClean()
Request::getStringParameter(string $paramName): string Response::isBufferEnabled(): bool
Request::getBoolParameter(string $paramName): bool Response::JsonContentType($http_status_code = 200)
Request::isParameterSet(string $paramName): bool Response::HtmlContentType($http_status_code = 200)
Request::getHttpRequestMethod(): string Response::TextContentType($http_status_code = 200)

For everything else, use the built-in functions of PHP., (*4)

Recommended IDE: PhpStorm, (*5)

Packagist page: https://packagist.org/packages/tbolner/flex-php-io, (*6)

Example usages

<?php declare(strict_types=1);

namespace MyProject;

use FlexPhpIO\Response;


Response::bufferStart();
Response::HtmlContentType();

echo '<html><body>

Hello World!, (*7)

</body></html>';
<?php declare(strict_types=1);

namespace MyProject;

use FlexPhpIO\Request;
use FlexPhpIO\Response;

try {
    Response::bufferStart();

    $input_data = Request::getPostedJson();
    $message = @trim((string)$input_data["message"]);

    if ($message == "") {
        throw new \Exception("Missing or invalid 'message' field in input.");
    }

    Response::printJson([
        "status" => "ok",
        "message" => $message
    ]);

} catch (\Exception $ex) {
    Response::printJson([
        "status" => "error",
        "message" => $ex->getMessage();
    ], 400);
}

An example for using it in a controller of FlexPhpRouter:, (*8)

  • project1/src/Controller/items.php
<?php declare(strict_types=1);

namespace MyProject\Controller;

use FlexPhpRouter\Router;
use FlexPhpIO\Response;


Router::get(
    "items/{id:int}/info",
    function (int $id) {
        Response::printJson([
            "item_id" => $id
        ]);
    }
);

Notes: - When the output buffering is enabled, then you can change the headers any time, even after you have printed some output. - When the output buffering is disable, then you cannot change the headers anymore after data was written to the output. - In the FlexPhpRouter example, the output buffering should've already been set up in the main initialization file or entry point., (*9)

Installation

{
    "require": {
        "tbolner/flex-php-io": "dev-master"
    }
}

Then execute:, (*10)

composer update

This library requires PHP 7.1 or newer., (*11)

The Versions

05/05 2018

dev-master

9999999-dev

IO library for web and CLI applications.

  Sources   Download

Apache-2.0

The Requires

  • php >=7.1

 

php cli web io content-type output-buffering