laravel-bridge / scratch
For project from scratch
Installs: 2 609
Dependents: 2
Suggesters: 0
Security: 0
Stars: 10
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: ^7.1 | ^8.0
- illuminate/config: ^5.6 | ^6 | ^7 | ^8
- illuminate/container: ^5.6 | ^6 | ^7 | ^8
- illuminate/support: ^5.6 | ^6 | ^7 | ^8
Requires (Dev)
- ext-pdo: *
- illuminate/database: ^5.6 | ^6 | ^7 | ^8
- illuminate/events: ^5.6 | ^6 | ^7 | ^8
- illuminate/filesystem: ^5.6 | ^6 | ^7 | ^8
- illuminate/http: ^5.6 | ^6 | ^7 | ^8
- illuminate/log: ^5.6 | ^6 | ^7 | ^8
- illuminate/pagination: ^5.6 | ^6 | ^7 | ^8
- illuminate/translation: ^5.6 | ^6 | ^7 | ^8
- illuminate/view: ^5.6 | ^6 | ^7 | ^8
- mikey179/vfsstream: ^1.6.7
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.2 | ^8 | ^9
- psy/psysh: ^0.10
- squizlabs/php_codesniffer: ^3.5.2
README
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
andilluminate/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
, requireilluminate/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
andilluminate/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 FacadeLog::driver($name)
to specify.$logger
is the instance implementedPsr\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.
- Setup method config or setup step
- 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
- Idea by @recca0120
- Logo by @ycs77