10000 Symfony 4.2 support + various improvements · gistol/symfony-vuejs@81fa28a · GitHub
[go: up one dir, main page]

Skip to content

Commit 81fa28a

Browse files
author
Julien Neuhart
committed
Symfony 4.2 support + various improvements
1 parent 5877b3c commit 81fa28a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4402
-2914
lines changed

app/.env.dist renamed to app/.env

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# This file is a "template" of which env vars need to be defined for your application
2-
# Copy this file to .env file for development, create environment variables when deploying to production
1+
# This file defines all environment variables that the application needs.
2+
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE.
3+
# Use ".env.local" for local overrides during development.
4+
# Use real environment variables when deploying to production.
35
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
46

57
###> symfony/framework-bundle ###
68
APP_ENV=dev
7-
APP_SECRET=8d2a5c935d8ef1c0e2b751147382bc75
9+
APP_SECRET=9bb5629914a418a2267cda32d336ded5
810
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
9-
#TRUSTED_HOSTS=localhost,example.com
11+
#TRUSTED_HOSTS='^localhost|example\.com$'
1012
###< symfony/framework-bundle ###
1113

1214
###> doctrine/doctrine-bundle ###

app/.env.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# define your env variables for the test env here
2+
KERNEL_CLASS='App\Kernel'
3+
APP_SECRET='s$cretf0rt3st'
4+
SYMFONY_DEPRECATIONS_HELPER=999999

app/.gitignore

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
###> symfony/framework-bundle ###
3 10000 -
/.env
3+
/.env.local
4+
/.env.*.local
45
/public/bundles/
56
/var/
67
/vendor/
@@ -15,14 +16,14 @@
1516
/.web-server-pid
1617
###< symfony/web-server-bundle ###
1718

18-
###> symfony/webpack-encore-pack ###
19-
/node_modules/
20-
/public/build/
21-
npm-debug.log
22-
yarn-error.log
23-
###< symfony/webpack-encore-pack ###
24-
2519
###> squizlabs/php_codesniffer ###
2620
/.phpcs-cache
2721
/phpcs.xml
2822
###< squizlabs/php_codesniffer ###
23+
24+
###> symfony/webpack-encore-bundle ###
25+
/node_modules/
26+
/public/build/
27+
npm-debug.log
28+
yarn-error.log
29+
###< symfony/webpack-encore-bundle ###

app/assets/vue/App.vue

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@
2626

2727
<script>
2828
import axios from 'axios';
29-
import router from './router';
3029
3130
export default {
3231
name: 'app',
33-
beforeMount () {
34-
let vueRouting = this.$parent.$el.attributes['data-vue-routing'].value,
35-
queryParameters = JSON.parse(this.$parent.$el.attributes['data-query-parameters'].value);
36-
37-
router.push({path: vueRouting, query: queryParameters});
38-
},
3932
created () {
33+
let isAuthenticated = JSON.parse(this.$parent.$el.attributes['data-is-authenticated'].value),
34+
roles = JSON.parse(this.$parent.$el.attributes['data-roles'].value);
35+
36+
let payload = { isAuthenticated: isAuthenticated, roles: roles };
37+
this.$store.dispatch('security/onRefresh', payload);
38+
4039
axios.interceptors.response.use(undefined, (err) => {
4140
return new Promise(() => {
4241
if (err.response.status === 403) {
4342
this.$router.push({path: '/login'})
43+
} else if (err.response.status === 500) {
44+
document.open();
45+
document.write(err.response.data);
46+
document.close();
4447
}
4548
throw err;
4649
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div class="alert alert-danger" role="alert">
3+
{{ error.response.data.error }}
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'errorMessage',
10+
props: ['error'],
11+
}
12+
</script>

app/assets/vue/store/security.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default {
66
isLoading: false,
77
error: null,
88
isAuthenticated: false,
9+
roles: [],
910
},
1011
getters: {
1112
isLoading (state) {
@@ -18,33 +19,49 @@ export default {
1819
return state.error;
1920
},
2021
isAuthenticated (state) {
21-
state.isAuthenticated = document.cookie.indexOf('authenticated') !== -1;
2222
return state.isAuthenticated;
2323
},
24+
hasRole (state) {
25+
return role => {
26+
return state.roles.indexOf(role) !== -1;
27+
}
28+
},
2429
},
2530
mutations: {
2631
['AUTHENTICATING'](state) {
2732
state.isLoading = true;
2833
state.error = null;
2934
state.isAuthenticated = false;
35+
state.roles = [];
3036
},
31-
['AUTHENTICATING_SUCCESS'](state) {
37+
['AUTHENTICATING_SUCCESS'](state, roles) {
3238
state.isLoading = false;
3339
state.error = null;
3440
state.isAuthenticated = true;
41+
state.roles = roles;
3542
},
3643
['AUTHENTICATING_ERROR'](state, error) {
3744
state.isLoading = false;
3845
state.error = error;
3946
state.isAuthenticated = false;
47+
state.roles = [];
48+
},
49+
['PROVIDING_DATA_ON_REFRESH_SUCCESS'](state, payload) {
50+
state.isLoading = false;
51+
state.error = null;
52+
state.isAuthenticated = payload.isAuthenticated;
53+
state.roles = payload.roles;
4054
},
4155
},
4256
actions: {
4357
login ({commit}, payload) {
4458
commit('AUTHENTICATING');
4559
return SecurityAPI.login(payload.login, payload.password)
46-
.then(() => commit('AUTHENTICATING_SUCCESS'))
60+
.then(res => commit('AUTHENTICATING_SUCCESS', res.data))
4761
.catch(err => commit('AUTHENTICATING_ERROR', err));
4862
},
63+
onRefresh({commit}, payload) {
64+
commit('PROVIDING_DATA_ON_REFRESH_SUCCESS', payload);
65+
},
4966
},
5067
}

app/assets/vue/views/Login.vue

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,36 @@
2525
</div>
2626

2727
<div v-else-if="hasError" class="row col">
28-
<div class="alert alert-danger" role="alert">
29-
{{ error }}
30-
</div>
28+
<error-message :error="error"></error-message>
3129
</div>
3230
</div>
3331
</template>
3432

3533
<script>
34+
import ErrorMessage from '../components/ErrorMessage';
35+
3636
export default {
3737
name: 'login',
38+
components: {
39+
ErrorMessage,
40+
},
3841
data () {
3942
return {
4043
login: '',
4144
password: '',
4245
};
4346
},
47+
created () {
48+
let redirect = this.$route.query.redirect;
49+
50+
if (this.$store.getters['security/isAuthenticated']) {
51+
if (typeof redirect !== 'undefined') {
52+
this.$router.push({path: redirect});
53+
} else {
54+
this.$router.push({path: '/home'});
55+
}
56+
}
57+
},
4458
computed: {
4559
isLoading () {
4660
return this.$store.getters['security/isLoading'];
@@ -60,9 +74,9 @@
6074
this.$store.dispatch('security/login', payload)
6175
.then(() => {
6276
if (typeof redirect !== 'undefined') {
63-
this.$router.push({path: redirect});
77+
this.$router.push({path: redirect});
6478
} else {
65-
this.$router.push({path: '/home'});
79+
this.$router.push({path: '/home'});
6680
}
6781
});
6882
},

app/assets/vue/views/Posts.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<h1>Posts</h1>
55
</div>
66

7-
<div class="row col">
7+
<div class="row col" v-if="canCreatePost">
88
<form>
99
<div class="form-row">
1010
<div class="col-8">
@@ -23,7 +23,7 @@
2323

2424
<div v-else-if="hasError" class="row col">
2525
<div class="alert alert-danger" role="alert">
26-
{{ error }}
26+
<error-message :error="error"></error-message>
2727
</div>
2828
</div>
2929

@@ -39,11 +39,13 @@
3939

4040
<script>
4141
import Post from '../components/Post';
42+
import ErrorMessage from '../components/ErrorMessage';
4243
4344
export default {
4445
name: 'posts',
4546
components: {
46-
Post
47+
Post,
48+
ErrorMessage,
4749
},
4850
data () {
4951
return {
@@ -69,6 +71,9 @@
6971
posts () {
7072
return this.$store.getters['post/posts'];
7173
},
74+
canCreatePost () {
75+
return this.$store.getters['security/hasRole']('ROLE_FOO');
76+
}
7277
},
7378
methods: {
7479
createPost () {

app/bin/console

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,36 @@ use App\Kernel;
55
use Symfony\Bundle\FrameworkBundle\Console\Application;
66
use Symfony\Component\Console\Input\ArgvInput;
77
use Symfony\Component\Debug\Debug;
8-
use Symfony\Component\Dotenv\Dotenv;
98

109
set_time_limit(0);
1110

12-
require __DIR__.'/../vendor/autoload.php';
11+
require dirname(__DIR__).'/vendor/autoload.php';
1312

1413
if (!class_exists(Application::class)) {
15-
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
14+
throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
1615
}
1716

18-
if (!isset($_SERVER['APP_ENV'])) {
19-
if (!class_exists(Dotenv::class)) {
20-
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
21-
}
22-
(new Dotenv())->load(__DIR__.'/../.env');
17+
$input = new ArgvInput();
18+
if (null !== $_ENV['APP_ENV'] = $input->getParameterOption(['--env', '-e'], null, true)) {
19+
putenv('APP_ENV='.$_ENV['APP_ENV']);
20+
// force loading .env files when --env is defined
21+
$_SERVER['APP_ENV'] = null;
2322
}
2423

25-
$input = new ArgvInput();
26-
$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true);
27-
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true);
24+
if ($input->hasParameterOption('--no-debug', true)) {
25+
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
26+
}
27+
28+
require dirname(__DIR__).'/config/bootstrap.php';
2829

29-
if ($debug) {
30+
if ($_SERVER['APP_DEBUG']) {
3031
umask(0000);
3132

3233
if (class_exists(Debug::class)) {
3334
Debug::enable();
3435
}
3536
}
3637

37-
$kernel = new Kernel($env, $debug);
38+
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
3839
$application = new Application($kernel);
3940
$application->run($input);

app/bin/phpunit

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ if (!file_exists(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-php
55
echo "Unable to find the `simple-phpunit` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
66
exit(1);
77
}
8-
if (false === getenv('SYMFONY_DEPRECATIONS_HELPER')) {
9-
// see https://symfony.com/doc/current/components/phpunit_bridge.html#making-tests-fail
10-
putenv('SYMFONY_DEPRECATIONS_HELPER=999999');
8+
9+
if (false === getenv('SYMFONY_PHPUNIT_VERSION')) {
10+
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
1111
}
1212
if (false === getenv('SYMFONY_PHPUNIT_REMOVE')) {
1313
putenv('SYMFONY_PHPUNIT_REMOVE=');
1414
}
15-
if (false === getenv('SYMFONY_PHPUNIT_VERSION')) {
16-
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
17-
}
1815
if (false === getenv('SYMFONY_PHPUNIT_DIR')) {
1916
putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
2017
}

0 commit comments

Comments
 (0)
0