Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit c3a8df7

Browse files
committed
✨ init v3
1 parent 6ee998f commit c3a8df7

File tree

11 files changed

+162
-138
lines changed

11 files changed

+162
-138
lines changed

App/Database/Factories/Factory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
namespace App\Database\Factories;
44

5+
use Faker\Generator;
56
use Leaf\Factory as Base;
67
use Illuminate\Support\Str;
78

89
/**
910
* Base Factory Class
1011
* ----------------
1112
* You can define methods here that would be used
12-
* throughout your factory classes.
13+
* throughout your factory classes.
1314
*/
1415
class Factory extends Base
1516
{
1617
public $str;
18+
/**@var Generator $faker*/
19+
public $faker;
1720

1821
public function __construct()
1922
{

App/Database/Factories/UserFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ class UserFactory extends Factory
1515
public function definition()
1616
{
1717
return [
18-
'username' => strtolower($this->faker->firstName),
19-
'name' => $this->faker->name,
18+
'id' => $this->faker->unique()->numberBetween(0, 200),
19+
'username' => strtolower(implode("-", explode(" ", $this->faker->name))),
20+
'fullname' => $this->faker->name,
2021
'email' => $this->faker->unique()->safeEmail,
2122
'email_verified_at' => \Leaf\Date::now(),
2223
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password

App/Database/Migrations/2019_11_18_133625_create_users.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ class CreateUsers extends Database {
1313
* @return void
1414
*/
1515
public function up() {
16-
// if (!static::$capsule::schema()->hasTable("users")):
17-
// static::$capsule::schema()->create("users", function (Blueprint $table) {
18-
// $table->increments('id');
19-
// $table->string('username');
20-
// $table->string('name');
21-
// $table->string('email')->unique();
22-
// $table->timestamp('email_verified_at')->nullable();
23-
// $table->string('password');
24-
// $table->rememberToken();
25-
// $table->timestamps();
26-
// });
27-
// endif;
16+
if (!static::$capsule::schema()->hasTable("users")):
17+
static::$capsule::schema()->create("users", function (Blueprint $table) {
18+
$table->increments('id');
19+
$table->string('username');
20+
$table->string('fullname');
21+
$table->string('email')->unique();
22+
$table->timestamp('email_verified_at')->nullable();
23+
$table->string('password');
24+
$table->rememberToken();
25+
$table->timestamps();
26+
});
27+
endif;
2828

29-
// you can now build your migrations with schemas
30-
Schema::build(static::$capsule, dirname(__DIR__) . "/Schema/users.json");
29+
// you can now build your migrations with schemas [EXPERIMENTAL]
30+
// Schema::build(static::$capsule, dirname(__DIR__) . "/Schema/users.json");
3131
}
3232

3333
/**

App/Database/Seeds/UsersSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function run()
1616
{
1717
// You can directly create db records
1818
// $user = new User();
19-
// $user->name = 'Mychi';
19+
// $user->fullname = 'Mychi Darko';
2020
// $user->email = "[email protected]";
2121
// $user->password = md5("password.demo");
2222
// $user->save();

App/Routes/_app.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
/**@var Leaf\App $app */
3+
use App\Models\User;
44

5-
$app->get("/", function () {
5+
app()->get("/", function () {
66
response(["message" => "Congrats!! You're on Leaf API"]);
77
});
88

9-
$app->get("/app", function () {
9+
app()->get("/app", function () {
1010
// app() returns $app
1111
response(app()->routes());
1212
});

App/Routes/_auth.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
$app->group("/auth", function () use ($app) {
4-
$app->post("/login", "Auth\LoginController@index");
5-
$app->post("/register", "Auth\RegisterController@store");
6-
$app->get("/logout", "Auth\LoginController@logout");
3+
app()->group("/auth", function () {
4+
app()->post("/login", "Auth\LoginController@index");
5+
app()->post("/register", "Auth\RegisterController@store");
6+
app()->get("/logout", "Auth\LoginController@logout");
77
// Reset and recover account will be added later
88
});
99

10-
$app->group("/user", function () use ($app) {
11-
$app->get("/", "Auth\AccountController@user");
12-
$app->post("/update", "Auth\AccountController@update");
10+
app()->group("/user", function () {
11+
app()->get("/", "Auth\AccountController@user");
12+
app()->post("/update", "Auth\AccountController@update");
1313
});

App/Routes/index.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
/**@var Leaf\App $app */
4-
53
/*
64
|--------------------------------------------------------------------------
75
| Set up 404 handler
@@ -10,7 +8,7 @@
108
| Create a handler for 404 errors
119
|
1210
*/
13-
$app->set404(function () {
11+
app()->set404(function () {
1412
response()->json("Resource not found", 404, true);
1513
});
1614

@@ -22,10 +20,10 @@
2220
| Create a handler for error 500
2321
|
2422
*/
25-
$app->setErrorHandler(function ($e = null) use($app) {
23+
app()->setErrorHandler(function ($e = null) {
2624
if ($e) {
27-
if ($app->config("log.enabled")) {
28-
$app->logger()->error($e);
25+
if (app()->config("log.enabled")) {
26+
app()->logger()->error($e);
2927
}
3028
}
3129

@@ -41,7 +39,7 @@
4139
| the controller namespace first.
4240
|
4341
*/
44-
$app->setNamespace("\App\Controllers");
42+
app()->setNamespace("\App\Controllers");
4543

4644
// You can break up routes into individual files
4745
require __DIR__ . "/_app.php";

Config/cors.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Configure allowed origins
7+
|--------------------------------------------------------------------------
8+
|
9+
| Configures the Access-Control-Allow-Origin CORS header. Possible values:
10+
|
11+
| * String - set origin to a specific origin. For example if
12+
| you set it to "http://example.com" only requests from
13+
| "http://example.com" will be allowed.
14+
|
15+
| * RegExp - set origin to a regular expression pattern which will be
16+
| used to test the request origin. If it's a match, the request origin
17+
| will be reflected. For example the pattern /example\.com$/ will reflect
18+
| any request that is coming from an origin ending with "example.com".
19+
|
20+
| * Array - set origin to an array of valid origins. Each origin can be a String
21+
| or a RegExp. For example ["http://example1.com", /\.example2\.com$/] will
22+
| accept any request from "http://example1.com" or from
23+
| a subdomain of "example2.com".
24+
|
25+
| * Function - set origin to a function implementing some custom
26+
| logic. The function takes the request origin as the first parameter
27+
| and a callback (called as callback(err, origin), where origin is a
28+
| non-function value of the origin option) as the second.
29+
|
30+
*/
31+
"origin" => "*",
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Configure allowed HTTP methods
36+
|--------------------------------------------------------------------------
37+
|
38+
| Configures the Access-Control-Allow-Methods CORS header.
39+
| Expects a comma-delimited string (ex: 'GET,PUT,POST') or
40+
| an array (ex: ['GET', 'PUT', 'POST'])
41+
|
42+
*/
43+
"methods" => "GET,HEAD,PUT,PATCH,POST,DELETE",
44+
45+
/*
46+
|--------------------------------------------------------------------------
47+
| Configure allowed HTTP headers
48+
|--------------------------------------------------------------------------
49+
|
50+
| Configures the Access-Control-Allow-Headers CORS header. Expects a
51+
| comma-delimited string (ex: 'Content-Type,Authorization') or
52+
| an array (ex: ['Content-Type', 'Authorization']). If not specified,
53+
| defaults to reflecting the headers specified in the request's
54+
| Access-Control-Request-Headers header.
55+
|
56+
*/
57+
"allowedHeaders" => "*",
58+
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Configure expose headers
62+
|--------------------------------------------------------------------------
63+
|
64+
| Configures the Access-Control-Expose-Headers CORS header. Expects
65+
| a comma-delimited string (ex: 'Content-Range,X-Content-Range')
66+
| or an array (ex: ['Content-Range', 'X-Content-Range']).
67+
| If not specified, no custom headers are exposed.
68+
|
69+
*/
70+
"exposedHeaders" => "",
71+
72+
/*
73+
|--------------------------------------------------------------------------
74+
| Configure credentials
75+
|--------------------------------------------------------------------------
76+
|
77+
| Configures the Access-Control-Allow-Credentials CORS header.
78+
| Set to true to pass the header, otherwise it is omitted.
79+
|
80+
*/
81+
"credentials" => false,
82+
83+
/*
84+
|--------------------------------------------------------------------------
85+
| Configure max age
86+
|--------------------------------------------------------------------------
87+
|
88+
| Configures the Access-Control-Max-Age CORS header. Set to
89+
| an integer to pass the header, otherwise it is omitted.
90+
|
91+
*/
92+
"maxAge" => null,
93+
94+
/*
95+
|--------------------------------------------------------------------------
96+
| Configure preflight continue
97+
|--------------------------------------------------------------------------
98+
|
99+
| Pass the CORS preflight response to the next handler.
100+
|
101+
*/
102+
"preflightContinue" => false,
103+
104+
/*
105+
|--------------------------------------------------------------------------
106+
| Log open
107+
|--------------------------------------------------------------------------
108+
|
109+
| Provides a status code to use for successful OPTIONS requests,
110+
| since some legacy browsers (IE11, various SmartTVs) choke on 204.
111+
|
112+
*/
113+
"optionsSuccessStatus" => 204,
114+
];

Config/functions.php

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,5 @@
11
<?php
22

3-
if (!function_exists('_env')) {
4-
/**
5-
* Gets the value of an environment variable.
6-
*
7-
* @param string $key
8-
* @param mixed $default
9-
* @return mixed
10-
*/
11-
function _env($key, $default = null)
12-
{
13-
$item = getenv($key);
14-
15-
if (!isset($_ENV[$key]) || (isset($_ENV[$key]) && $_ENV[$key] == null)) {
16-
$item = $default;
17-
}
18-
19-
return $item;
20-
}
21-
}
22-
23-
if (!function_exists('app')) {
24-
/**
25-
* Return the Leaf instance
26-
*
27-
* @return Leaf\App
28-
*/
29-
function app()
30-
{
31-
$app = Leaf\Config::get("app")["instance"] ?? null;
32-
33-
if (!$app) {
34-
$app = new Leaf\App;
35-
Leaf\Config::set("app", ["instance" => $app]);
36-
}
37-
38-
return $app;
39-
}
40-
}
41-
423
if (!function_exists('auth')) {
434
/**
445
* Return Leaf's auth object
@@ -135,32 +96,6 @@ function hasAuth()
13596
}
13697
}
13798

138-
if (!function_exists('request')) {
139-
/**
140-
* Return request or request data
141-
*
142-
* @param array|string $data — Get data from request
143-
*/
144-
function request($data = null)
145-
{
146-
if ($data) return app()->request()->get($data);
147-
return app()->request();
148-
}
149-
}
150-
151-
if (!function_exists('response')) {
152-
/**
153-
* Return response or set response data
154-
*
155-
* @param array|string $data — The JSON response to set
156-
*/
157-
function response($data = null)
158-
{
159-
if ($data) return app()->response()->json($data);
160-
return app()->response();
161-
}
162-
}
163-
16499
if (!function_exists('Route')) {
165100
/**
166101
* @param string The request method(s)
@@ -213,34 +148,3 @@ function setHeader($key, $value = "", $replace = true, $code = 200)
213148
app()->headers()->set($key, $value, $replace, $code);
214149
}
215150
}
216-
217-
if (!function_exists("view")) {
218-
function view(string $view, array $data = [])
219-
{
220-
if (ViewConfig("render")) {
221-
ViewConfig("config")([
222-
"views_path" => AppConfig("views.path"),
223-
"cache_path" => AppConfig("views.cachePath"),
224-
]);
225-
226-
return ViewConfig("render")($view, $data);
227-
}
228-
229-
$engine = ViewConfig("view_engine");
230-
231-
$className = strtolower(get_class(new $engine));
232-
233-
$fullName = explode("\\", $className);
234-
$className = $fullName[count($fullName) - 1];
235-
236-
if (forward_static_call_array(["Leaf\\View", $className], [])) {
237-
forward_static_call_array(["Leaf\\View", $className], [])->configure(AppConfig("views.path"), AppConfig("views.cachePath"));
238-
return forward_static_call_array(["Leaf\\View", $className], [])->render($view, $data);
239-
}
240-
241-
$engine = new $engine($engine);
242-
$engine->config(AppConfig("views.path"), AppConfig("views.cachePath"));
243-
244-
return $engine->render($view, $data);
245-
}
246-
}

0 commit comments

Comments
 (0)