Skip to content

Instantly share code, notes, and snippets.

@Pen-y-Fan
Last active April 30, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pen-y-Fan/b14a811462c7ce9cc1e7903e78b1c825 to your computer and use it in GitHub Desktop.
Save Pen-y-Fan/b14a811462c7ce9cc1e7903e78b1c825 to your computer and use it in GitHub Desktop.
Laravel Base Project

Laravel Base Project

Automatically create a new laravel project with PHPStan, Easy Coding Standard and IDE Helper (PhpStorm).

Requirments

  • Windows OS
  • Laragon (recommended) (laragon.org)
  • PHP (included with Laragon)
    • pdo-sqlite ext enabled (not enabled, by default. To enable: Laragon menu > PHP > Extensions > tick pdo-sqlite)
  • Composer (included with Laragon)
  • Git (included with Laragon)
  • laravel installer globally installed (composer global require laravel/installer)
  • PhpStorm (recommended), or alternativly VS Code (recommended).

Setup

Copy the files from this gist to the following directories:

C:\laragon\usr\bin (or any directory which is in the Windows PATH Environment variable)

  • Project.bat

C:\laragon\usr\share

  • .env
  • AddScriptsToComposer.php
  • ecs.yaml
  • phpstan.neon

Note: If laragon is installed to a different location or drive edit Project.bat and change the line set laragon=c:\laragon to the location of the laragon directory e.g. set laragon=d:\laragon

Check Laragon has already been added to the PATH system variable. Laragon menu > Tools > Path > Manage Path. There should be three sections: Lagaon, User and System. Check laragon is added to the user path (do not add twice):

It will look similar to this:

--------------- Laragon ---------------
C:\laragon\bin;
C:\laragon\bin\apache\httpd-2.4.41-win64-VS16\bin;
C:\laragon\bin\code;
C:\laragon\bin\composer;
C:\laragon\bin\git\bin;
C:\laragon\bin\git\cmd;
C:\laragon\bin\git\mingw64\bin;
C:\laragon\bin\git\usr\bin;
C:\laragon\bin\laragon\utils;
C:\laragon\bin\mysql\mysql-5.7.24-winx64\bin;
C:\laragon\bin\nginx\nginx-1.16.0;
C:\laragon\bin\ngrok;
C:\laragon\bin\nodejs\node-v12;
C:\laragon\bin\notepad++;
C:\laragon\bin\php\php-7.4.5-Win32-vc15-x64;
C:\laragon\bin\putty;
C:\laragon\bin\redis\redis-x64-3.2.100;
C:\laragon\bin\telnet;
C:\laragon\usr\bin;
...
--------------- User ---------------
C:\laragon\bin;
C:\laragon\bin\apache\httpd-2.4.41-win64-VS16\bin;
C:\laragon\bin\code;
C:\laragon\bin\composer;
C:\laragon\bin\git\bin;
C:\laragon\bin\git\cmd;
C:\laragon\bin\git\mingw64\bin;
C:\laragon\bin\git\usr\bin;
C:\laragon\bin\laragon\utils;
C:\laragon\bin\mysql\mysql-5.7.24-winx64\bin;
C:\laragon\bin\nginx\nginx-1.16.0;
C:\laragon\bin\ngrok;
C:\laragon\bin\nodejs\node-v12;
C:\laragon\bin\notepad++;
C:\laragon\bin\php\php-7.4.5-Win32-vc15-x64;
C:\laragon\bin\putty;
C:\laragon\bin\redis\redis-x64-3.2.100;
C:\laragon\bin\telnet;
C:\laragon\usr\bin;
...
--------------- System ---------------
%SystemRoot%\system32;
%SystemRoot%;
...

If Laragon is not added to the User path, either use commander from the laragon menu (which will have the PATH automatically added by Laragon), or menu > Add Laragon to Path. You may need to log off and back on for the path to stick.

Use

TL;DR version:

Run:

project.bat
  1. When prompted type in the name of the Laravel project to be created. e.g. laravel-project
  2. Wait for the project to be created.
  3. cd into the project and launch your IDE/code editor.

Detailed Description

The windows batch file Project.bat will automatically create a laravel project by taking the following actions:

  1. Open a command prompt
  2. Type Project
  3. Choose a name for the laravel project
  4. The directory is checked, if it is existing the batch file will display the error
  5. laravel new "project" is run
  6. PHPStan, Easy Coding Standard, IDE Helper (for PHPStorm) are composer required
  7. git is inisalised and the project files added
  8. PhpStorm IDE helper files are created
  9. .editorconfig is appended with:
    • [*.html]
    • indent_size = 2
  10. cc.bat, fc.bat, pu.bat & ps.bat batch files are created for ecs, phpstan and phpunit
  11. .gitignore is appended with /.idea, /report.xml, /.phpstorm.meta.php, /_ide_helper.php and /_ide_helper_models.php
  12. Base configs files for ECS and PHPStan are copied: ecs.yaml and phpstan.neon
  13. Scripts are added to composer.json
  14. git is inisalised, project files added and a base commit created.
  15. Easy Coding standrd (ECS) is run 10 times
  16. git add is run again and changed files commited

The project can be started.

Code Standard

Easy Coding Standard (ECS) can be used to check for style and code standards, PSR-12 is used.

Check Code

To check code, but not fix an errors:

composer check-cs

A Windows batch file has been created, similar to an alias on Linux/Mac (e.g. alias cc="composer check-cs"), the same ECS composer check-cs can be run:

cc

Fix Code

May code fixes are automatically provided by ECS, if advised to run --fix, the following script can be run:

composer fix-cs

A Windows a batch file has been created, similar to an alias on Linux/Mac (e.g. alias fc="composer fix-cs"), the same ECS composer fix-cs can be run:

fc

Static Analysis

PHPStan can be used to run static analysis checks:

composer phpstan

The same PHPUnit composer phpstan can be run:

ps

Happy coding!

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:SVo74sBj6xXSiT27wLqUjh+simp8c3icSrjCvlnOH7I=
APP_DEBUG=true
APP_URL=http://127.0.0.1
LOG_CHANNEL=stack
DB_CONNECTION=sqlite
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
<?php
declare(strict_types=1);
/**
* @author michael.pen.y.Fan@gmail.com
* @license MIT
*
* Automated method to add scripts to composer.json
* Normally used in conjunction with Project.bat
*
* Usage:
* php AddScriptsToComposer.php <filepathToProject>
* e.g.
* php AddScriptsToComposer.php c:\laragon\www\MyNewProject\
* Note: The path must have a suffix of \
*/
// $argv[1] is the file path to the project
if (!$argv[1]) {
throw new exception("No file path provided");
}
$fileName = $argv[1] . "composer.json";
$composerJson = file_get_contents($fileName);
if ($composerJson === false) {
$e = new exception("Failed to read file: '$fileName'");
$e->fileName = $fileName;
throw $e;
}
$composerArray = json_decode($composerJson, true);
$composerArray["scripts"]["checkcode"] = "phpcs app tests --standard=PSR12";
$composerArray["scripts"]["fixcode"] = "phpcbf app tests --standard=PSR12";
$composerArray["scripts"]["test"] = "phpunit";
$composerArray["scripts"]["tests"] = "phpunit";
$composerArray["scripts"]["test-coverage"] = "phpunit --coverage-html build/coverage";
$composerArray["scripts"]["check-cs"] = "ecs check app tests --ansi";
$composerArray["scripts"]["fix-cs"] = "ecs check app tests --fix --ansi";
$composerArray["scripts"]["phpstan"] = "phpstan analyse --ansi";
$newComposerJson = json_encode($composerArray, JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT);
if (file_put_contents($fileName, $newComposerJson) === false) {
throw new exception("Failed to write file: '$fileName'");
};
parameters:
sets:
- 'psr12'
- 'php71'
- 'symplify'
- 'common'
- 'clean-code'
line_ending: "\n"
# 4 spaces
indentation: " "
skip:
Symplify\CodingStandard\Sniffs\Architecture\DuplicatedClassShortNameSniff: null
# Allow snake_case for tests
PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff:
- tests/**
# Ignore what is not owned - Laravel's TestCase class does not start with 'Abstract'
Symplify\CodingStandard\Sniffs\Naming\AbstractClassNameSniff:
- tests/TestCase.php
# Ignore what is not owned - Laravel's traits do not end with 'Trait'
Symplify\CodingStandard\Sniffs\Naming\TraitNameSniff:
- tests/**
# Ignore what is not owned - CommentedOutCodeSniff
Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff.Found:
- app/Console/Kernel.php
# Ignore what is not owned - @return \Illuminate\Contracts\Validation\Validator
SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff.ReferenceViaFullyQualifiedName:
- app/Http/Controllers/Auth/RegisterController.php
services:
Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
max_cognitive_complexity: 8
includes:
- vendor/nunomaduro/larastan/extension.neon
- vendor/symplify/phpstan-extensions/config/config.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
paths:
- app
- tests
# The level 8 is the highest level
level: 8
checkGenericClassInNonGenericObjectType: false
# Larstan recommendation:
checkMissingIterableValueType: false
# to allow installing with various phsptan versions without reporting old errors here
reportUnmatchedIgnoredErrors: false
ignoreErrors:
# Larstan recommendation:
- '#Unsafe usage of new static#'
# Ignore errors in Laravel's Middleware classes
-
message: '#Method App\\Http\\Middleware\\RedirectIfAuthenticated\:\:handle\(\) has no return typehint specified#'
path: app\Http\Middleware\RedirectIfAuthenticated.php
-
message: '#Method App\\Http\\Middleware\\Authenticate\:\:redirectTo\(\) should return string\|null but return statement is missing#'
path: app\Http\Middleware\Authenticate.php
# buggy
# mixed
# cache buggy
# tests
# iterable
@echo off
set cwd=%cd%
set laragon=c:\laragon
:menu
echo.
echo Enter the name of your new Laravel project:
set /p project=Enter name (quit to exit):
If /I "%project%"=="" goto error
If /I "%project%"=="quit" goto quit
IF EXIST %laragon%\www\%project%\ (
goto projectExists
)
cd /d %laragon%\www
echo on
call laravel new %project%
@echo off
echo.
echo %project% created
echo.
cd %project%
echo on
call composer require --dev --prefer-dist phpstan/phpstan phpstan/phpstan-phpunit symplify/easy-coding-standard symplify/phpstan-extensions nunomaduro/larastan emielmolenaar/phpcs-laravel barryvdh/laravel-ide-helper
@echo off
call php artisan ide-helper:generate
rem for ide-helper:models to run there must be a valid database, a sqlite one will be temporarily created
call fsutil file createnew database\database.sqlite 0
rename .env .env.temp
copy %laragon%\usr\share\.env
call php artisan ide-helper:models --write
del .env
rename .env.temp .env
call php artisan ide-helper:meta
echo. > .editorconfig
echo [*.html] >> .editorconfig
echo indent_size = 2 >> .editorconfig
echo composer test > pu.bat
echo composer check-cs > cc.bat
echo composer fix-cs > fc.bat
echo composer phpstan > ps.bat
echo /.idea >> .gitignore
echo /report.xml >> .gitignore
echo /.phpstorm.meta.php >> .gitignore
echo /_ide_helper.php >> .gitignore
echo /_ide_helper_models.php >> .gitignore
xcopy %laragon%\usr\share\ecs.yaml *.* /D /Y
xcopy %laragon%\usr\share\phpstan.neon *.* /D /Y
call php %laragon%\usr\share\AddScriptsToComposer.php %laragon%\www\%project%\
rem On windows git init converts the files from LF to CRLF - ECS will automatically fix this
call git init
call git add .
call git commit -m "Base Laravel app"
rem run fix code 10x to fix most of the standard issues with Laravel's default setup
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
call vendor\bin\ecs check app tests --fix --no-ansi
rem Fixed files can now be added to git
call git add .
call git commit -m "Initial commit"
echo .
echo Project created
echo ======= =======
echo Location: %laragon%\www\%project%\
echo.
goto end
:error
echo The project name can not be empty
goto menu
:projectExists
echo There is already a project located at %laragon%\www\%project%\
:quit
echo No project created
:end
cd /d %cwd%
set cwd=
set project=
set laragon=
echo on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment