mzdr/micro

A tiny multi-tool for your next big PHP adventure.

2.1.1 2019-09-16 16:23 UTC

README

micro

A tiny multi-tool for your next big PHP adventure.1


Features

  • Easily manage your application’s configuration with Gestalt.

    Supports PHP, YAML, INI and JSON files out of the box.

  • Handle various databases with Medoo.

    Medoo supports several SQL databases2, lots of common and complex SQL queries, data mapping and prevention of SQL injection.

  • Have error handling and formatting to your preference with BooBoo.

    BooBoo is an error handler for PHP that allows for the execution of handlers and formatters for viewing and managing errors in development and production. It won’t end up in your stack trace, is built for logging, designed for extension and handles errors non-blocking by default.

  • Set up lightning fast routing with FastRoute.

    A fast regular expression based request router for PHP. See this article for more details.

  • Use native PHP templates with Plates.

    Plates is designed for developers who prefer to use native PHP templates over compiled template languages, such as Twig or Blade. It supports layouts, inheritance, namespaces, data sharing and comes with built-in escaping helpers.

  • ⚡️ Speed up your (dynamic) web applications by caching with Scrapbook.

    PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.


Requirements

PHP 7.1.3+ and preferably URL Rewriting enabled.


Installation

It is recommended that you install this framework using Composer.

composer require mzdr/micro

Usage

micro is basically just a bunch of wrapper functions located under a single namespace called µ. Every file you see in ./micro/functions is also available as a function with the same name.

Let me talk in code to you… 😎

<?php

// Returns the instance of the Gestalt (@samrap/gestalt) library.
µ\config();

// You can pretty much do anything you like with it.
$config = µ\config();
$special_var = $config->get('my.stored.variable');


// Need to register routes with the
// FastRoute (@nikic/FastRoute) instance?
µ\router()->get('/home', function () {

    // 🌈 Use your imagination…

    // How about we use the Plates
    // (@thephpleague/plates) template engine? 🤩
    echo µ\template()->render('home');
});


// Tired of typing µ? 😫 Join the club!
namespace µ {
    router()->get('/', function () {
        $key = 'my-heavy-op';
        $ttl = 300;
        $value = "cached for $ttl seconds.";

        if (cache()->has($key) === false) {
            sleep(2); // So so heavy…

            cache()->set($key, $value, $ttl);

            return $value;
        }

        return cache()->get($key);
    });
}

// Out there in strange places? 👽 Import it!
namespace alien {
    use function µ\config;

    $done = config()->get('get.it.done');
}

Just follow the official documentation of each library listed below or jump into the ./micro/functions folder to get a look under the hood.

Function Documentation
µ\config() https://github.com/samrap/gestalt-docs
µ\database() https://medoo.in/doc
µ\error() http://booboo.thephpleague.com/
µ\router() https://github.com/nikic/FastRoute
µ\template() http://platesphp.com/
µ\cache() https://www.scrapbook.cash/

Bootstrapping

You’re in a hurry? Bootstrap a blank, ready-to-view µ project!

  1. Make new project directory and jump into it.

    mkdir fancy-project && cd $_
  2. Install µ.

    composer require mzdr/micro
  3. Boostrap it.

    ./vendor/mzdr/micro/bootstrap
  4. That’s it! Now how do you check out your project?

    • Create a virtual host and point the document root to the public folder, or…

    • Fire up PHP’s built-in web server, or
      (Doesn’t support .htaccess, you have to include assets without $this->asset(…) cache busting)

    • Just browse to the public folder via your local webserver.
      (You probably need to adjust µ.paths.public in your configs/master.yaml)


License

This project is licensed under MIT license.




1 It may be tiny and powerful, but it’s not the right tool for every job.
2 MySQL, MSSQL, SQLite, MariaDB, PostgreSQL, Sybase, Oracle and many more. catfan/Medoo#features