Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Symfony full stack revamp #1

Merged
merged 36 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b3186b4
feat: require Symfony framework bundle
dkarlovi Dec 31, 2021
66550fd
cleanup
dkarlovi Dec 31, 2021
0481a24
Symfony fullstack CLI app
dkarlovi Dec 31, 2021
607e379
Symfony Encore, YAML, etc
dkarlovi Dec 31, 2021
b553135
YASSG project base dir injection
dkarlovi Dec 31, 2021
714ebe6
Rename Encore config
dkarlovi Dec 31, 2021
0625077
Cleanup
dkarlovi Dec 31, 2021
73f5659
Symfony front controller
dkarlovi Dec 31, 2021
4322a55
Routing + rendering
dkarlovi Dec 31, 2021
93f6d63
Cleanup
dkarlovi Dec 31, 2021
d2c36f0
Project config loading + routes
dkarlovi Dec 31, 2021
5604e87
Database
dkarlovi Dec 31, 2021
196e995
Route attributes in the controller
dkarlovi Dec 31, 2021
9e00cd2
Index
dkarlovi Dec 31, 2021
91b65da
Improve query error message
dkarlovi Dec 31, 2021
ef83b4b
Config validation and merging
dkarlovi Jan 1, 2022
beb0ee7
Remove Symfony framework commands
dkarlovi Jan 1, 2022
1b8a491
Generator
dkarlovi Jan 1, 2022
92a2ab8
Generator
dkarlovi Jan 2, 2022
37eadb1
Symfony bug (?) workaround
dkarlovi Jan 2, 2022
3181780
Remove bug workaround
dkarlovi Jan 2, 2022
d357566
Move Makefile to YASSG repo
dkarlovi Jan 3, 2022
5a8fc49
Init resources
dkarlovi Jan 3, 2022
f4c93b7
Cleanup
dkarlovi Jan 3, 2022
d7667a3
Cleanup
dkarlovi Jan 3, 2022
981d33b
Cleanup
dkarlovi Jan 3, 2022
d1331b9
Make config optional
dkarlovi Jan 3, 2022
11af4ed
feat: Init command
dkarlovi Jan 3, 2022
fdec680
feat: Init command, assets
dkarlovi Jan 3, 2022
87b4f5d
feat: Init command, more assets
dkarlovi Jan 3, 2022
3c5829b
feat: Init command, demo page
dkarlovi Jan 3, 2022
f1db66a
feat: Init command, demo page
dkarlovi Jan 3, 2022
fec0c32
feat: Init command, demo page cleanup
dkarlovi Jan 3, 2022
0c27594
feat: Init command, don't ignore hidden files
dkarlovi Jan 3, 2022
e163a9a
fix: assets paths in dev and prod
dkarlovi Jan 3, 2022
9dde111
Asset normalize path
dkarlovi Jan 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions bin/yassg
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#!/usr/bin/env php
<?php

use Symfony\Component\Console\Input\ArgvInput;
use function Sigwin\YASSG\bootstrap;
use function Sigwin\YASSG\createRenderer;
use Sigwin\YASSG\Bridge\Symfony\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;

require __DIR__ .'/../bootstrap.php';
$baseDir = bootstrap([
__DIR__.'/../../../autoload.php',
]);
require_once __DIR__ .'/../bootstrap.php';

$input = new ArgvInput();
$baseUrl = ($input->getParameterOption('--base-url') ?: 'file://'. $baseDir .'/public');
return function (array $context) use ($baseDir) {
$kernel = new Kernel($GLOBALS['YASSG_BASEDIR'], $context['APP_ENV'], (bool) $context['APP_DEBUG']);

$renderer = createRenderer($baseDir, $baseUrl);
$generator = new \Sigwin\YASSG\Generator($renderer);

$buildDir = $baseDir.'/'.(trim($input->getFirstArgument() ?? 'build', '/'));
$generator->generate($buildDir);
return new Application($kernel);
};
55 changes: 16 additions & 39 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,23 @@
use Sigwin\YASSG\Router;
use Symfony\Component\Yaml\Yaml;

ini_set('display_errors', 1);

function bootstrap(array $autoLoaders): string
{
$found = false;
$baseDir = null;
foreach ($autoLoaders as $autoLoader) {
if (true === file_exists($autoLoader)) {
/* @noinspection PhpIncludeInspection */
include $autoLoader;

// strips "vendor/"
$baseDir = realpath(dirname($autoLoader).'/..');

$found = true;
}
$autoloaders = [
__DIR__.'/../../autoload_runtime.php',
];

$baseDir = null;
foreach ($autoloaders as $autoloader) {
if (true === file_exists($autoloader)) {
$GLOBALS['YASSG_BASEDIR'] = $baseDir = realpath(dirname($autoloader).'/..');

require_once $autoloader;

break;
}
if ($found === false) {
fwrite(STDERR, 'You must set up the project dependencies using `composer install`'.PHP_EOL);

exit(1);
}

return $baseDir;
}

function createRenderer(string $baseDir, string $baseUrl): \Sigwin\YASSG\Renderer
{
$database = new Database(Yaml::parseFile($baseDir.'/database.yaml'));
$router = new Router(Yaml::parseFile($baseDir.'/routes.yaml'), $baseUrl);
$renderer = new Renderer(
$database,
$router,
[
'templates' => $baseDir.'/templates',
'base_url' => $baseUrl,
'cache' => $baseDir.'/cache',
'auto_reload' => true,
]
);

return $renderer;
if ($baseDir === null) {
fwrite(STDERR, 'You must set up the project dependencies using `composer install`'.PHP_EOL);

exit(1);
}
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
"bentools/cartesian-product": "^1.3",
"symfony/console": "^6.0",
"symfony/expression-language": "^6.0",
"symfony/http-foundation": "^6.0",
"symfony/routing": "^6.0",
"symfony/yaml": "^6.0",
"twig/twig": "^3.3"
"symfony/framework-bundle": "^6.0",
"symfony/runtime": "^6.0",
"symfony/twig-bundle": "^6.0",
"symfony/webpack-encore-bundle": "^1.13",
"symfony/yaml": "^6.0"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"symfony/runtime": true
}
},
"bin": [
"bin/yassg"
Expand Down
7 changes: 7 additions & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
];
3 changes: 3 additions & 0 deletions config/packages/assets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
framework:
assets:
json_manifest_path: '%sigwin_yassg.base_dir%/public/assets/manifest.json'
3 changes: 3 additions & 0 deletions config/packages/twig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
twig:
# TODO: extract to config
default_path: "%sigwin_yassg.base_dir%/templates"
3 changes: 3 additions & 0 deletions config/packages/webpack_encore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webpack_encore:
# TODO: extract to config
output_path: "%sigwin_yassg.base_dir%/public/assets"
3 changes: 3 additions & 0 deletions config/routes/yassg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yassg:
resource: 'Sigwin\YASSG\Bridge\Symfony\Routing\Loader\RouteLoader'
type: service
46 changes: 46 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
services:
_defaults:
autowire: true
autoconfigure: true
bind:
$baseDir: "%sigwin_yassg.base_dir%"
$data: "%sigwin_yassg.database%"
$routes: "%sigwin_yassg.routes%"

Sigwin\YASSG\:
resource: '../src/'
exclude:
- '../src/Kernel.php'

Sigwin\YASSG\Bridge\Symfony\Controller\:
resource: '../src/Bridge/Symfony/Controller'
tags: ['controller.service_arguments']

Sigwin\YASSG\Bridge\Symfony\Command\InitCommand:
$initDir: "%kernel.project_dir%/resources/init"

Sigwin\YASSG\Bridge\Twig\Extension\AssetExtension:
autoconfigure: false

when@prod:
services:
Sigwin\YASSG\Bridge\Symfony\Routing\Generator\FilenameUrlGenerator:
decorates: Symfony\Component\Routing\Generator\UrlGeneratorInterface
arguments:
$urlGenerator: '@Sigwin\YASSG\Bridge\Symfony\Routing\Generator\FilenameUrlGenerator.inner'

router.request_context:
class: Symfony\Component\Routing\RequestContext
factory: ['@Sigwin\YASSG\Bridge\Symfony\Routing\BuildRequestContextFactory', 'create']
shared: false

Sigwin\YASSG\Bridge\Twig\Extension\AssetExtension:
decorates: twig.extension.assets
arguments:
$requestContext: '@router.request_context'
$packages: '@assets.packages'

twig.extension.routing:
class: Symfony\Bridge\Twig\Extension\RoutingExtension
arguments:
$generator: '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
30 changes: 30 additions & 0 deletions resources/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BASE_URL ?= "file://${APP_ROOT}/${BUILD_DIR}"

start/dev: dev
dev: clean
@make dev/assets dev/server -j2
dev/server: vendor index.php
php -S localhost:${APP_PORT}
dev/assets: node_modules
node_modules/.bin/encore dev-server
index.php:
ln -s vendor/sigwin/yassg/web/index.php

build: ${BUILD_DIR}/assets/entrypoints.json vendor
php vendor/sigwin/yassg/bin/yassg yassg:generate --env prod $(BASE_URL)
build/clean: clean ${BUILD_DIR}/assets/entrypoints.json vendor
php vendor/sigwin/yassg/bin/yassg yassg:generate --env prod $(BASE_URL)
.PHONY: build build/clean

${BUILD_DIR}:
mkdir -p ${BUILD_DIR}
${BUILD_DIR}/assets/entrypoints.json: | ${BUILD_DIR} node_modules
node_modules/.bin/encore production

clean:
rm -rf cache ${BUILD_DIR}
.PHONY: clean
node_modules:
npm install
vendor:
composer install
5 changes: 5 additions & 0 deletions resources/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/cache/
/node_modules/
/public/
/vendor/
/index.php
53 changes: 53 additions & 0 deletions resources/init/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
stages:
- assets
- pages

assets:
stage: assets
image: node:16.4.2-alpine
script:
- npm install
- node_modules/.bin/encore production
artifacts:
paths:
- public
cache:
key:
files:
- package-lock.json
paths:
- node_modules/

.generate: &generate
stage: pages
image: jakzal/phpqa:php8.0-alpine
needs:
- assets
script:
- mkdir node_modules
- composer install
- make build
artifacts:
paths:
- public
cache:
key:
files:
- composer.lock
paths:
- vendor/

preview:
<<: *generate
variables:
# TODO: simplify with https://gitlab.com/gitlab-org/gitlab/-/issues/36373
BASE_URL: https://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-/engineering/${CI_PROJECT_NAME}/-/jobs/${CI_JOB_ID}/artifacts/public
except:
- master

pages:
<<: *generate
variables:
BASE_URL: ${CI_PAGES_URL}
only:
- master
7 changes: 7 additions & 0 deletions resources/init/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.SILENT:

APP_PORT := 9988
APP_ROOT := $(abspath $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
BUILD_DIR := public

include vendor/sigwin/yassg/resources/Makefile
5 changes: 5 additions & 0 deletions resources/init/assets/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import './styles/app.scss';

document.addEventListener('DOMContentLoaded', () => {
console.log('DOM ready!');
});
14 changes: 14 additions & 0 deletions resources/init/assets/images/sigwin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/init/assets/styles/app.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: #eee;
}
6 changes: 6 additions & 0 deletions resources/init/config/database.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sigwin_yassg:
database:
collections:
demo_page:
title: Sigwin YASSG demo page
image: assets/images/sigwin.svg
Loading