Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Introducing: Facades!
Browse files Browse the repository at this point in the history
  • Loading branch information
leocavalcante committed Nov 26, 2019
1 parent 37ca8f1 commit b1c9600
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 6 deletions.
31 changes: 31 additions & 0 deletions bin/generate-facades.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Siler;

use function Siler\File\recur_iter_dir;
use function Siler\Functional\flatten;

$basedir = dirname(__DIR__);
require_once "$basedir/vendor/autoload.php";

$files = recur_iter_dir("$basedir/src/", '/\.php$/');
$contents = array_map('file_get_contents', $files);

$facades = array_map(function (string $content): array {
preg_match_all('/namespace (.*);/', $content, $matches);
$namespace = $matches[1][0] ?? '';

preg_match_all('/\nfunction ([A-z0-9_]+)\(/', $content, $matches);

if (empty($matches[1])) {
return [];
}

return ["namespace $namespace;", array_map(function (string $funcName) use ($namespace): string {
return "const $funcName = '\\$namespace\\$funcName';";
}, $matches[1]), "\n"];
}, $contents);

$lines = flatten($facades);
array_unshift($lines, "<?php declare(strict_types=1);\n");
file_put_contents("$basedir/src/facades.php", join("\n", $lines));
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"@composer lint",
"@composer phan",
"@composer unit"
]
],
"generate-facades": "php bin/generate-facades.php"
},
"suggest": {
"vlucas/phpdotenv": "Storing configuration in the environment is one of the tenets of a twelve-factor app."
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

<file>src</file>
<file>tests</file>

<exclude-pattern>src/facades.php</exclude-pattern>
</ruleset>
5 changes: 4 additions & 1 deletion src/File/File.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Siler\File;

Expand Down Expand Up @@ -45,6 +47,7 @@ function recur_iter_dir(string $dirname, string $regex = '/.*/', $mode = \RegexI
*/
function concat_files(array $files): string
{
$files = array_map('strval', $files);
$files = array_map('file_get_contents', $files);
return trim(array_reduce($files, concat("\n"), ''));
}
1 change: 0 additions & 1 deletion src/GraphQL/SubscriptionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use GraphQL\Type\Schema;
use Siler\Container;
use Siler\Encoder\Json;

use function Siler\array_get;

/**
Expand Down
280 changes: 279 additions & 1 deletion src/facades.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,285 @@
<?php declare(strict_types=1);

namespace Siler\Functional;
namespace Siler\Container;
const get = '\Siler\Container\get';
const set = '\Siler\Container\set';
const has = '\Siler\Container\has';
const clear = '\Siler\Container\clear';
const inject = '\Siler\Container\inject';
const retrieve = '\Siler\Container\retrieve';


namespace Siler\Db;
const connect = '\Siler\Db\connect';
const query = '\Siler\Db\query';
const prepare = '\Siler\Db\prepare';
const error = '\Siler\Db\error';
const fetch_all = '\Siler\Db\fetch_all';
const fetch = '\Siler\Db\fetch';
const mysql_dsn = '\Siler\Db\mysql_dsn';


namespace Siler\Mongo;
const connect = '\Siler\Mongo\connect';
const database = '\Siler\Mongo\database';
const collection = '\Siler\Mongo\collection';
const oid = '\Siler\Mongo\oid';
const using = '\Siler\Mongo\using';
const find = '\Siler\Mongo\find';
const find_one = '\Siler\Mongo\find_one';
const insert_many = '\Siler\Mongo\insert_many';
const insert_one = '\Siler\Mongo\insert_one';
const update_one = '\Siler\Mongo\update_one';
const update_many = '\Siler\Mongo\update_many';
const delete_one = '\Siler\Mongo\delete_one';
const delete_many = '\Siler\Mongo\delete_many';
const __get_dbname_or_throw = '\Siler\Mongo\__get_dbname_or_throw';


namespace Siler\Redis;
const connect = '\Siler\Redis\connect';
const get = '\Siler\Redis\get';
const set = '\Siler\Redis\set';
const has = '\Siler\Redis\has';


namespace Siler\Diactoros;
const request = '\Siler\Diactoros\request';
const response = '\Siler\Diactoros\response';
const html = '\Siler\Diactoros\html';
const json = '\Siler\Diactoros\json';
const text = '\Siler\Diactoros\text';
const redirect = '\Siler\Diactoros\redirect';
const none = '\Siler\Diactoros\none';


namespace Siler\Dotenv;
const init = '\Siler\Dotenv\init';
const env = '\Siler\Dotenv\env';
const int_val = '\Siler\Dotenv\int_val';
const bool_val = '\Siler\Dotenv\bool_val';
const requires = '\Siler\Dotenv\requires';


namespace Siler\Encoder\Json;
const encode = '\Siler\Encoder\Json\encode';
const decode = '\Siler\Encoder\Json\decode';


namespace Siler\File;
const recursively_iterated_directory = '\Siler\File\recursively_iterated_directory';
const recur_iter_dir = '\Siler\File\recur_iter_dir';
const concat_files = '\Siler\File\concat_files';


namespace Siler\Functional;
const identity = '\Siler\Functional\identity';
const always = '\Siler\Functional\always';
const equal = '\Siler\Functional\equal';
const less_than = '\Siler\Functional\less_than';
const greater_than = '\Siler\Functional\greater_than';
const if_else = '\Siler\Functional\if_else';
const match = '\Siler\Functional\match';
const any = '\Siler\Functional\any';
const all = '\Siler\Functional\all';
const not = '\Siler\Functional\not';
const add = '\Siler\Functional\add';
const mul = '\Siler\Functional\mul';
const sub = '\Siler\Functional\sub';
const div = '\Siler\Functional\div';
const mod = '\Siler\Functional\mod';
const compose = '\Siler\Functional\compose';
const bool = '\Siler\Functional\bool';
const noop = '\Siler\Functional\noop';
const hold = '\Siler\Functional\hold';
const puts = '\Siler\Functional\puts';
const flatten = '\Siler\Functional\flatten';
const head = '\Siler\Functional\head';
const last = '\Siler\Functional\last';
const tail = '\Siler\Functional\tail';
const init = '\Siler\Functional\init';
const uncons = '\Siler\Functional\uncons';
const non_null = '\Siler\Functional\non_null';
const non_empty = '\Siler\Functional\non_empty';
const partial = '\Siler\Functional\partial';
const if_then = '\Siler\Functional\if_then';
const is_empty = '\Siler\Functional\is_empty';
const isnull = '\Siler\Functional\isnull';
const concat = '\Siler\Functional\concat';


namespace Siler\Functional\Monad;
const identity = '\Siler\Functional\Monad\identity';
const maybe = '\Siler\Functional\Monad\maybe';


namespace Siler\GraphQL;
const debug = '\Siler\GraphQL\debug';
const init = '\Siler\GraphQL\init';
const input = '\Siler\GraphQL\input';
const execute = '\Siler\GraphQL\execute';
const promise_execute = '\Siler\GraphQL\promise_execute';
const psr7 = '\Siler\GraphQL\psr7';
const schema = '\Siler\GraphQL\schema';
const resolvers = '\Siler\GraphQL\resolvers';
const subscriptions_manager = '\Siler\GraphQL\subscriptions_manager';
const subscriptions = '\Siler\GraphQL\subscriptions';
const subscriptions_at = '\Siler\GraphQL\subscriptions_at';
const publish = '\Siler\GraphQL\publish';
const listen = '\Siler\GraphQL\listen';
const val = '\Siler\GraphQL\val';
const enum = '\Siler\GraphQL\enum';
const field = '\Siler\GraphQL\field';
const str = '\Siler\GraphQL\str';
const int = '\Siler\GraphQL\int';
const float = '\Siler\GraphQL\float';
const bool = '\Siler\GraphQL\bool';
const list_of = '\Siler\GraphQL\list_of';
const id = '\Siler\GraphQL\id';
const itype = '\Siler\GraphQL\itype';
const type = '\Siler\GraphQL\type';


namespace Siler\Http;
const cookie = '\Siler\Http\cookie';
const session = '\Siler\Http\session';
const setsession = '\Siler\Http\setsession';
const flash = '\Siler\Http\flash';
const redirect = '\Siler\Http\redirect';
const url = '\Siler\Http\url';
const path = '\Siler\Http\path';
const uri = '\Siler\Http\uri';


namespace Siler\Http\Request;
const raw = '\Siler\Http\Request\raw';
const params = '\Siler\Http\Request\params';
const json = '\Siler\Http\Request\json';
const headers = '\Siler\Http\Request\headers';
const header = '\Siler\Http\Request\header';
const get = '\Siler\Http\Request\get';
const post = '\Siler\Http\Request\post';
const input = '\Siler\Http\Request\input';
const file = '\Siler\Http\Request\file';
const method = '\Siler\Http\Request\method';
const method_is = '\Siler\Http\Request\method_is';
const accepted_locales = '\Siler\Http\Request\accepted_locales';
const recommended_locale = '\Siler\Http\Request\recommended_locale';
const bearer = '\Siler\Http\Request\bearer';
const authorization_header = '\Siler\Http\Request\authorization_header';


namespace Siler\Http\Response;
const output = '\Siler\Http\Response\output';
const text = '\Siler\Http\Response\text';
const html = '\Siler\Http\Response\html';
const jsonstr = '\Siler\Http\Response\jsonstr';
const json = '\Siler\Http\Response\json';
const header = '\Siler\Http\Response\header';
const redirect = '\Siler\Http\Response\redirect';
const no_content = '\Siler\Http\Response\no_content';
const cors = '\Siler\Http\Response\cors';


namespace Siler\HttpHandlerRunner;
const sapi_emit = '\Siler\HttpHandlerRunner\sapi_emit';


namespace Siler\SwiftMailer;
const send = '\Siler\SwiftMailer\send';
const message = '\Siler\SwiftMailer\message';
const smtp = '\Siler\SwiftMailer\smtp';
const mailer = '\Siler\SwiftMailer\mailer';


namespace Siler\Monolog;
const stream = '\Siler\Monolog\stream';
const log = '\Siler\Monolog\log';
const handler = '\Siler\Monolog\handler';
const debug = '\Siler\Monolog\debug';
const info = '\Siler\Monolog\info';
const notice = '\Siler\Monolog\notice';
const warning = '\Siler\Monolog\warning';
const error = '\Siler\Monolog\error';
const critical = '\Siler\Monolog\critical';
const alert = '\Siler\Monolog\alert';
const emergency = '\Siler\Monolog\emergency';


namespace Siler\Str;
const slugify = '\Siler\Str\slugify';
const lines = '\Siler\Str\lines';


namespace Siler\Tuple;
const tuple = '\Siler\Tuple\tuple';


namespace Siler\Ratchet;
const graphql_subscriptions = '\Siler\Ratchet\graphql_subscriptions';


namespace Siler\Result;
const success = '\Siler\Result\success';
const failure = '\Siler\Result\failure';


namespace Siler\Route;
const get = '\Siler\Route\get';
const post = '\Siler\Route\post';
const put = '\Siler\Route\put';
const delete = '\Siler\Route\delete';
const options = '\Siler\Route\options';
const any = '\Siler\Route\any';
const route = '\Siler\Route\route';
const method_path = '\Siler\Route\method_path';
const regexify = '\Siler\Route\regexify';
const resource = '\Siler\Route\resource';
const routify = '\Siler\Route\routify';
const files = '\Siler\Route\files';
const class_name = '\Siler\Route\class_name';
const stop_propagation = '\Siler\Route\stop_propagation';
const cancel = '\Siler\Route\cancel';
const canceled = '\Siler\Route\canceled';
const resume = '\Siler\Route\resume';
const match = '\Siler\Route\match';
const did_match = '\Siler\Route\did_match';
const purge_match = '\Siler\Route\purge_match';


namespace Siler;
const array_get = '\Siler\array_get';
const require_fn = '\Siler\require_fn';


namespace Siler\Stratigility;
const process = '\Siler\Stratigility\process';
const pipe = '\Siler\Stratigility\pipe';
const handle = '\Siler\Stratigility\handle';


namespace Siler\Swoole;
const http_handler = '\Siler\Swoole\http_handler';
const http = '\Siler\Swoole\http';
const request = '\Siler\Swoole\request';
const response = '\Siler\Swoole\response';
const emit = '\Siler\Swoole\emit';
const json = '\Siler\Swoole\json';
const websocket_hooks = '\Siler\Swoole\websocket_hooks';
const websocket = '\Siler\Swoole\websocket';
const push = '\Siler\Swoole\push';
const broadcast = '\Siler\Swoole\broadcast';
const cors = '\Siler\Swoole\cors';
const raw = '\Siler\Swoole\raw';
const no_content = '\Siler\Swoole\no_content';
const not_found = '\Siler\Swoole\not_found';
const graphql_subscriptions = '\Siler\Swoole\graphql_subscriptions';
const bearer = '\Siler\Swoole\bearer';
const http_server_port = '\Siler\Swoole\http_server_port';
const graphql_handler = '\Siler\Swoole\graphql_handler';


namespace Siler\Twig;
const init = '\Siler\Twig\init';
const render = '\Siler\Twig\render';

4 changes: 3 additions & 1 deletion tests/Unit/File/FileTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Siler\Test\Unit\File;

Expand Down
1 change: 0 additions & 1 deletion tests/Unit/GraphQL/SubscriptionsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Siler\GraphQL\SubscriptionsConnection;
use Siler\GraphQL\SubscriptionsManager;
use stdClass;

use const Siler\GraphQL\GQL_CONNECTION_INIT;
use const Siler\GraphQL\GQL_DATA;
use const Siler\GraphQL\GQL_START;
Expand Down

0 comments on commit b1c9600

Please sign in to comment.