For project from scratch

1.7.0 2021-03-21 05:44 UTC

README

banner

tests codecov Codacy Badge Latest Stable Version Total Downloads License

Start Laravel project from scratch.

Installation

Run the following command to require package:

composer require laravel-bridge/scratch

Usage

Setup when you want to use the package

Database

Require illuminate/database and illuminate/events

Method setupDatabaseConfig() has 3 arguments, the following is signature:

public function setupDatabaseConfig(string $name, array $connection, bool $default = false);
  • $name is the database name.
  • $connection is the database config only.
  • $default will set the default database if true.

Method setupDatabaseConfigs() has 2 arguments, the following is signature:

public function setupDatabaseConfig(array $connections, string $default = 'default');
  • $connections is the all connections config.
  • $default specify the connection is default.

Examples

index.php example for Database:

use LaravelBridge\Scratch\Application;

$connections = [
    'driver' => 'sqlite',
    'database' => __DIR__ . '/sqlite.db',
];

$app = Application::getInstance()
    ->setupDatabaseConfig('default', $connections, true)
    ->bootstrap();

Eloquent is easy, too.

use Illuminate\Database\Eloquent\Model;

class User extends Model
{

}

// ---

$user = new User();
$user->username = 'root';
$user->password = 'password';

$user->save();

User::all()->toArray();

View

Require illuminate/view, require illuminate/translation when need translation.

index.php example for View:

use LaravelBridge\Scratch\Application;

Application::getInstance()
    ->setupTranslator(__DIR__ . '/lang')
    ->setupView(__DIR__, __DIR__ . '/compiled')
    ->withFacades()
    ->bootstrap();

echo View::make('view', ['rows' => [1, 2, 3]]);

Template example view.blade.php:

@foreach ($rows as $row)
    {{ $row }}
@endforeach

Logging

Require illuminate/log and illuminate/events

Method setupLogger() has 3 arguments, the following is signature:

public function setupLogger(string $name, LoggerInterface $logger, bool $default = false);
  • $name is the Log name, and use Facade Log::driver($name) to specify.
  • $logger is the instance implemented Psr\Log\LoggerInterface.
  • $default will set the default log driver if true.

Here is a testing example:

$spy = new TestHandler();

$logger = new Monolog\Logger('test');
$logger->pushHandler($spy);

$this->target->setupLogger('test', $logger, true)
    ->bootstrap();

Log::info('log_test');

$this->assertTrue($spy->hasInfoRecords());

Configuration

The configuration will use illuminate/config package. Following is the priority.

  1. Setup method config or setup step
  2. Configuration Loader or bootstrap step

Facade

Use withFacades() to active Facade and register short class:

$app->withFacades();

View::make(); // It's works

Bootstrap

Bootstrap is a lifecycle in Laravel Kernel. The following is bootstrapper order.

\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class
\Illuminate\Foundation\Bootstrap\HandleExceptions::class
\Illuminate\Foundation\Bootstrap\RegisterFacades::class
\Illuminate\Foundation\Bootstrap\RegisterProviders::class
\Illuminate\Foundation\Bootstrap\BootProviders::class

In Scratch application, we can load config functionally. and use withFacades() to register Facade first. finally, call ServiceProvider::register() on every provider when call bootstrap(). Next, call ServiceProvider::boot() on every provider, just like Laravel Kernel.

bootstrap() has an argument $withAllLaravelProviders, register all laravel provider when true. Also, It's default true. However, use withoutLaravelProvider() if you don't want use some Laravel providers.

Example Projects or Libraries

Projects:

Libraries:

Thanks