laravel-bridge/scratch

For project from scratch
2,774 10
Install
composer require laravel-bridge/scratch
Latest Version:1.7.0
PHP:^7.1 | ^8.0
License:MIT
Last Updated:Mar 21, 2021
Links: GitHub  ·  Packagist
Maintainer: MilesChou

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

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

637,827,751 9,703
laravel/framework

The Laravel Framework.

589,172,971 34,950
laravel/tinker

Powerful REPL for the Laravel framework.

498,216,183 7,442
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

413,412,504 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released PHP Laravel License
1.7.0 ^7.1 | ^8.0 ^5.6 | ^6 | ^7 | ^8 MIT
1.6.0 ^7.1 ^5.6 | ^6 | ^7 | ^8 MIT
v1.5.0 >=7.1 ^5.6 | ^6 | ^7 | ^8 MIT
v1.4.0 >=7.1 ^5.6 | ^6 | ^7 | ^8 MIT
v1.3.0 >=7.1 ^5.6 | ^6 | ^7 | ^8 MIT