{"id":20731083,"url":"https://github.com/greg-md/php-framework","last_synced_at":"2025-03-11T10:21:11.636Z","repository":{"id":25875532,"uuid":"29315729","full_name":"greg-md/php-framework","owner":"greg-md","description":"Greg Framework provides a lightweight engine for fast creating powerful apps.","archived":false,"fork":false,"pushed_at":"2019-07-23T22:17:33.000Z","size":674,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-17T18:44:05.122Z","etag":null,"topics":["framework","greg-framework","greg-md","greg-php","php","php-framework","web-artisans"],"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/greg-md.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":"2015-01-15T20:05:25.000Z","updated_at":"2019-07-23T22:17:35.000Z","dependencies_parsed_at":"2022-08-06T07:15:59.116Z","dependency_job_id":null,"html_url":"https://github.com/greg-md/php-framework","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-framework","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-framework/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-framework/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-framework/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greg-md","download_url":"https://codeload.github.com/greg-md/php-framework/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243012750,"owners_count":20221616,"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":["framework","greg-framework","greg-md","greg-php","php","php-framework","web-artisans"],"created_at":"2024-11-17T05:13:39.068Z","updated_at":"2025-03-11T10:21:11.600Z","avatar_url":"https://github.com/greg-md.png","language":"PHP","readme":"# Greg PHP Framework\n\n[![StyleCI](https://styleci.io/repos/29315729/shield?style=flat)](https://styleci.io/repos/29315729)\n[![Build Status](https://travis-ci.org/greg-md/php-framework.svg)](https://travis-ci.org/greg-md/php-framework)\n[![Total Downloads](https://poser.pugx.org/greg-md/php-framework/d/total.svg)](https://packagist.org/packages/greg-md/php-framework)\n[![Latest Stable Version](https://poser.pugx.org/greg-md/php-framework/v/stable.svg)](https://packagist.org/packages/greg-md/php-framework)\n[![Latest Unstable Version](https://poser.pugx.org/greg-md/php-framework/v/unstable.svg)](https://packagist.org/packages/greg-md/php-framework)\n[![License](https://poser.pugx.org/greg-md/php-framework/license.svg)](https://packagist.org/packages/greg-md/php-framework)\n\nGreg Framework provides a lightweight engine for fast creating powerful apps.\n\n[Greg PHP Application](https://github.com/greg-md/php-app) is a prepared and deploy-ready application that you can use\nto achieve maximum productivity of that framework.\n\n# Table of Contents\n\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [How It Works](#how-it-works)\n* [Config](#config)\n* [Bootstrapping](#bootstrapping)\n* [Events](#events)\n    * [Listeners](#listeners)\n    * [Fire Events](#fire-events)\n    * [Events Objects](#events-objects)\n    * [Builtin Events](#builtin-events)\n* [License](#license)\n* [Huuuge Quote](#huuuge-quote)\n\n# Requirements\n\n* PHP Version `^7.1`\n\n# Installation\n\n`composer require greg-md/php-framework`\n\n# How It Works\n\nAll you need is to instantiate a new Application and run it from your chosen environment.\n\n```php\n\u003c?php\n\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n$app = new \\Greg\\Framework\\Application();\n\n$app-\u003erun(function () {\n    echo 'Hello World!';\n});\n```\n\nYou can also construct the Application with custom [Config](#config) and an [IoC Container](https://github.com/greg-md/php-dependency-injection).\n\n```php\n\u003c?php\n\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n$config = new \\Greg\\Framework\\Config([\n    'name' =\u003e 'John',\n]);\n\n$ioc = new \\Greg\\DependencyInjection\\IoCContainer();\n\n$ioc-\u003eregister($config);\n\n$app = new \\Greg\\Framework\\Application($config, $ioc);\n\n$app-\u003erun(function (\\Greg\\Framework\\Config $config) {\n    echo 'Hello ' . $config['name'] . '!';\n});\n```\n\nYeah, the previous example practically doesn't do nothing because you didn't use any features.\nBut let's look on the next examples when we want to create an Application\nthat should be run in [Browser](#running-for-http-requests) or in [Console](#running-for-console-requests).\n\n### Running for HTTP Requests\n\nFor HTTP Requests we can use the builtin `\\Greg\\Framework\\Http\\HttpKernel`\nthat is working with [HTTP Routes](https://github.com/greg-md/php-router).\n\nIn the next example we will instantiate the kernel and create a router that will say hello.\n\n\u003e Providing the Application and Router to the kernel is optional.\n\u003e If you will not provide them, the kernel will instantiate them by itself.\n\n```php\n$httpKernel = new \\Greg\\Framework\\Http\\HttpKernel($app);\n\n$router = $httpKernel-\u003erouter();\n\n$router-\u003eget('/', function() {\n    return 'Hello World!';\n});\n\n$httpResponse = $httpKernel-\u003erun();\n\n$httpResponse-\u003esend();\n```\n\n### Running for Console Requests\n\nFor Console Requests we can use the builtin `\\Greg\\Framework\\Console\\ConsoleKernel`\nthat is working with [Symfony Console Component](http://symfony.com/doc/current/components/console.html).\n\nIn the next example we will instantiate the kernel and create a command that will say hello.\n\n\u003e Providing the Application and Symfony Console Application to the kernel is optional.\n\u003e If you will not provide them, the kernel will instantiate them by itself.\n\n```php\n$consoleKernel = new \\Greg\\Framework\\Console\\ConsoleKernel($app);\n\n$helloCommand = new Symfony\\Component\\Console\\Command\\Command();\n$helloCommand-\u003esetName('hello');\n$helloCommand-\u003esetDescription('Say Hello.');\n$helloCommand-\u003esetCode(function(Symfony\\Component\\Console\\Input\\InputInterface $input, Symfony\\Component\\Console\\Output\\OutputInterface $output) {\n    $output-\u003ewriteln('Hello World!');\n});\n\n$consoleKernel-\u003eaddCommand($helloCommand);\n\n$responseCode = $consoleKernel-\u003erun();\n\nexit($responseCode);\n```\n\n# Config\n\nYou can define application configurations using the `\\Greg\\Framework\\Config` class.\nIt uses the [`ArrayAccessTrait`](https://github.com/greg-md/php-support/blob/master/docs/Accessor/ArrayAccessTrait.md)\nand can act as an array.\n\n```php\n$config = new \\Greg\\Framework\\Config([\n    'foo' =\u003e 'FOO',\n    'bar' =\u003e 'BAR',\n    'db' =\u003e [\n        'username' =\u003e 'foousername',\n        'password' =\u003e 'foosecret',\n    ],\n]);\n\n$config-\u003eget('foo'); // result: 'FOO'\n// or\n$config['bar']; // result: 'BAR'\n\n$config-\u003egetIndex('db.username'); // result: 'foousername'\n// or\n$config['db.password']; // result: 'foosecret'\n```\n\n# Bootstrapping\n\nTo keep the consistency of the application, you can define/register/load it's components into the bootstrap classes.\n\nA bootstrap class should be an instance of `\\Greg\\Framework\\BootstrapStrategy` that requires the `boot` method.\n\n```php\nclass AppBootstrap extends \\Greg\\Framework\\BootstrapAbstract\n{\n    public function boot(\\Greg\\Framework\\Application $app)\n    {\n        $app-\u003einject('redis.client', function() {\n            return new Redis();\n        });\n    }\n}\n```\n\nFor a better experience you can extend the `\\Greg\\Framework\\BootstrapAbstract` class.\nIt will allow you to define multiple boots in a specific order.\nEach method in this class that starts with `boot` in a `lowerCamelCase` format will be called. \n\n```php\nclass AppBootstrap extends \\Greg\\Framework\\BootstrapAbstract\n{\n    public function bootFoo()\n    { \n        $this-\u003edependsOn('redis'); // Call `bootRedis` method first.\n\n        $redis = $this-\u003eapp()-\u003eget('redis.client');\n\n        // ...\n    }\n\n    public function bootRedis()\n    {\n        $redis = new Redis();\n\n        $this-\u003eapp()-\u003einject('redis.client', $redis);\n    }\n}\n```\n\n***Next***, you have to add the bootstrap to the application.\n\n```php\n$app-\u003eaddBootstrap(new AppBootstrap());\n```\n\nThe same way you can define and create bootstraps only for the HTTP and Console kernels,\nusing the `Greg\\Framework\\Console\\BootstrapStrategy` and `Greg\\Framework\\Http\\BootstrapStrategy` strategies,\nor extending the `Greg\\Framework\\Console\\BootstrapAbstract` and `Greg\\Framework\\Http\\BootstrapAbstract` classes.\n\n```php\n$httpKernel-\u003eaddBootstrap(new AppHttpBootstrap());\n```\n\n```php\n$consoleKernel-\u003eaddBootstrap(new AppConsoleBootstrap());\n```\n\n# Events\n\nYou can define and fire events in your application.\n\n### Listeners\n\nListeners could be a callable, an object or a class name.\n\n```php\n$app-\u003elisten('my.event', function() {\n    // do the bussiness logic...\n});\n```\n\nObjects and class names requires the `handle` method that will be fired.\n\n```php\nclass MyListener\n{\n    public function handle() {\n        // do the bussiness logic...\n    }\n}\n```\n\n```php\n$app-\u003elisten('my.event', MyListener::class);\n// or\n$app-\u003elisten('my.event', new MyListener());\n```\n\n### Fire Events\n\nYou can fire events with or without custom arguments.\n\n```php\n$app-\u003efire('my.event');\n\n// or\n\n$user = new User();\n\n$app-\u003efire('my.event', $user);\n\n// or\n\n$app-\u003efireArgs('my.event', [new User()]);\n```\n\n### Events Objects\n\nFollowing the best practices, you can define events as objects.\n\nLet say we have a `LoginEvent` class that requires an `User`.\n\n```php\nclass LoginEvent\n{\n    private $user;\n\n    public function __construct(User $user)\n    {\n        $this-\u003euser = $user;\n    }\n    \n    public function user()\n    {\n        return $this-\u003euser;\n    }\n}\n```\n\nYou can define listeners using the class name as event name.\n\n```php\n$app-\u003elisten(LoginEvent::class, function(LoginEvent $event) {\n    // do the bussiness logic...\n});\n```\n\nAnd fire event in application.\n\n```php\n$app-\u003eevent(new LoginEvent($user));\n// or\n$app-\u003efire(LoginEvent::class, ...[new LoginEvent($user)]);\n```\n\n### Builtin Events\n\n#### Application\n\n* `\\Greg\\Framework\\Application::EVENT_RUN` - Fired **before** the application is ran;\n* `\\Greg\\Framework\\Application::EVENT_FINISHED` - Fired **after** the application is ran.\n\n#### **HTTP Kernel**\n\n* `\\Greg\\Framework\\Http\\HttpKernel::EVENT_RUN` - Fired **before** the kernel is ran;\n* `\\Greg\\Framework\\Http\\HttpKernel::EVENT_DISPATCHING` - Fired **before** the route is dispatched;\n* `\\Greg\\Framework\\Http\\HttpKernel::EVENT_DISPATCHED` - Fired **after** the route is dispatched;\n* `\\Greg\\Framework\\Http\\HttpKernel::EVENT_FINISHED` - Fired **after** the kernel is ran.\n\n#### **Console Kernel**\n\n* `\\Greg\\Framework\\Console\\ConsoleKernel::EVENT_RUN` - Fired **before** the kernel is ran;\n* `\\Greg\\Framework\\Console\\ConsoleKernel::EVENT_FINISHED` - Fired **v** the kernel is ran.\n\n# License\n\nMIT © [Grigorii Duca](http://greg.md)\n\n# Huuuge Quote\n\n![I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. \u0026copy; #horrorsquad](http://greg.md/huuuge-quote-fb.jpg)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-md%2Fphp-framework","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreg-md%2Fphp-framework","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-md%2Fphp-framework/lists"}