From 4548bea20543596a23c3326bfdc72ac6ab3af964 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sat, 9 Jul 2016 22:16:42 +0200 Subject: [PATCH 1/8] Add support for local config. --- .travis.yml | 5 ++++- README.md | 4 ++-- composer.json | 8 +++++++- config/.gitignore | 1 + config/console.php | 2 +- config/templates/console-local.php | 7 +++++++ config/{db.php => templates/db-local.php} | 3 +++ config/templates/web-local.php | 13 +++++++++++++ config/web.php | 2 +- tests/codeception/config/acceptance.php | 1 + tests/codeception/config/functional.php | 1 + tests/codeception/config/unit.php | 1 + web/index.php | 5 ++++- yii | 5 ++++- 14 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 config/.gitignore create mode 100644 config/templates/console-local.php rename config/{db.php => templates/db-local.php} (73%) create mode 100644 config/templates/web-local.php diff --git a/.travis.yml b/.travis.yml index 24b5d54ae..b3c7c70cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,10 @@ install: - travis_retry composer global require "codeception/codeception=2.0.*" "codeception/specify=*" "codeception/verify=*" # setup application: - | - sed -i "s/'cookieValidationKey' => ''/'cookieValidationKey' => 'testkey'/" config/web.php + cp config/templates/console-local.php config/console-local.php + cp config/templates/web-local.php config/web-local.php + cp config/templates/db-local.php config/db-local.php + sed -i "s/'cookieValidationKey' => ''/'cookieValidationKey' => 'testkey'/" config/web-local.php cd tests codecept build cd .. diff --git a/README.md b/README.md index 598f9637b..bdb08ea3b 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ INSTALLATION Extract the archive file downloaded from [yiiframework.com](http://www.yiiframework.com/download/) to a directory named `basic` that is directly under the Web root. -Set cookie validation key in `config/web.php` file to some random secret string: +Set cookie validation key in `config/web-local.php` file to some random secret string: ```php 'request' => [ @@ -84,7 +84,7 @@ CONFIGURATION ### Database -Edit the file `config/db.php` with real data, for example: +Edit the file `config/db-local.php` with real data, for example: ```php return [ diff --git a/composer.json b/composer.json index 4ad378784..e91427ac4 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,12 @@ "process-timeout": 1800 }, "scripts": { + "post-install-cmd": [ + "php -r \"if(!file_exists('config/console-local.php')){copy('config/templates/console-local.php', 'config/console-local.php');}\"", + "php -r \"if(!file_exists('config/web-local.php')){copy('config/templates/web-local.php', 'config/web-local.php');}\"", + "php -r \"if(!file_exists('config/db-local.php')){copy('config/templates/db-local.php', 'config/db-local.php');}\"", + "yii\\composer\\Installer::postCreateProject" + ], "post-create-project-cmd": [ "yii\\composer\\Installer::postCreateProject" ] @@ -43,7 +49,7 @@ } ], "generateCookieValidationKey": [ - "config/web.php" + "config/web-local.php" ] }, "asset-installer-paths": { diff --git a/config/.gitignore b/config/.gitignore new file mode 100644 index 000000000..34c7d59a1 --- /dev/null +++ b/config/.gitignore @@ -0,0 +1 @@ +/*-local.php diff --git a/config/console.php b/config/console.php index 7306e2764..3e1677e59 100644 --- a/config/console.php +++ b/config/console.php @@ -3,7 +3,7 @@ Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception'); $params = require(__DIR__ . '/params.php'); -$db = require(__DIR__ . '/db.php'); +$db = require(__DIR__ . '/db-local.php'); $config = [ 'id' => 'basic-console', diff --git a/config/templates/console-local.php b/config/templates/console-local.php new file mode 100644 index 000000000..1c56d8ad9 --- /dev/null +++ b/config/templates/console-local.php @@ -0,0 +1,7 @@ + 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2basic', diff --git a/config/templates/web-local.php b/config/templates/web-local.php new file mode 100644 index 000000000..31cf38950 --- /dev/null +++ b/config/templates/web-local.php @@ -0,0 +1,13 @@ + [ + 'request' => [ + // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation + 'cookieValidationKey' => '', + ], + ], +]; diff --git a/config/web.php b/config/web.php index 90a82876b..3a5b6d10a 100644 --- a/config/web.php +++ b/config/web.php @@ -37,7 +37,7 @@ ], ], ], - 'db' => require(__DIR__ . '/db.php'), + 'db' => require(__DIR__ . '/db-local.php'), /* 'urlManager' => [ 'enablePrettyUrl' => true, diff --git a/tests/codeception/config/acceptance.php b/tests/codeception/config/acceptance.php index c688575b6..49960cf96 100644 --- a/tests/codeception/config/acceptance.php +++ b/tests/codeception/config/acceptance.php @@ -4,6 +4,7 @@ */ return yii\helpers\ArrayHelper::merge( require(__DIR__ . '/../../../config/web.php'), + require(__DIR__ . '/../../../config/web-local.php'), require(__DIR__ . '/config.php'), [ diff --git a/tests/codeception/config/functional.php b/tests/codeception/config/functional.php index 6d22bd97b..12d75d7db 100644 --- a/tests/codeception/config/functional.php +++ b/tests/codeception/config/functional.php @@ -7,6 +7,7 @@ */ return yii\helpers\ArrayHelper::merge( require(__DIR__ . '/../../../config/web.php'), + require(__DIR__ . '/../../../config/web-local.php'), require(__DIR__ . '/config.php'), [ 'components' => [ diff --git a/tests/codeception/config/unit.php b/tests/codeception/config/unit.php index 5bab5eaec..eec68ef2b 100644 --- a/tests/codeception/config/unit.php +++ b/tests/codeception/config/unit.php @@ -4,6 +4,7 @@ */ return yii\helpers\ArrayHelper::merge( require(__DIR__ . '/../../../config/web.php'), + require(__DIR__ . '/../../../config/web-local.php'), require(__DIR__ . '/config.php'), [ diff --git a/web/index.php b/web/index.php index d1e070a39..c0988ed17 100644 --- a/web/index.php +++ b/web/index.php @@ -7,6 +7,9 @@ require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); -$config = require(__DIR__ . '/../config/web.php'); +$config = yii\helpers\ArrayHelper::merge( + require(__DIR__ . '/../config/web.php'), + require(__DIR__ . '/../config/web-local.php') +); (new yii\web\Application($config))->run(); diff --git a/yii b/yii index fc7090f77..e509646b7 100755 --- a/yii +++ b/yii @@ -14,7 +14,10 @@ defined('YII_ENV') or define('YII_ENV', 'dev'); require(__DIR__ . '/vendor/autoload.php'); require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php'); -$config = require(__DIR__ . '/config/console.php'); +$config = yii\helpers\ArrayHelper::merge( + require(__DIR__ . '/config/console.php'), + require(__DIR__ . '/config/console-local.php') +); $application = new yii\console\Application($config); $exitCode = $application->run(); From afe4bc31af8a4ce5050a9f79c0340f8d8d93bae8 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sat, 20 Aug 2016 13:28:02 +0200 Subject: [PATCH 2/8] Add docs about local config files to README.md --- README.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 384656ecc..9180d817b 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,24 @@ http://localhost/basic/web/ CONFIGURATION ------------- +### Local config files + +Usually each application has some configuration that should not be shared between different installations +and should not be stored in [version control system](https://en.wikipedia.org/wiki/Version_control), for +example personal keys or configuration specific for a particular server. In `config` directory you can find +a set of config files prefixed by `-local.php` - these files are designed for storing such a configuration. +These local configs are added to `.gitignore` and never will be pushed to source code repository, so you can safely +use it for override some general config. For example `config/web-local.php` will override some settings +from `config/web.php` (see [ArrayHelper::merge()](http://www.yiiframework.com/doc-2.0/guide-helper-array.html#merging-arrays) +for more details). Then `config/web.php` contains general configuration of web application shared with +all installations and `config/web-local.php` contains configuration specific only for local installation. + +In `config/templates` directory you can find templates for local config files. On first run `composer install` +these files will be copied to `config` directory. You can edit these templates to adjust default content of +local config files, but **you should not store any private data in templates** - these should be put into +`config/*-local.php` files after installation. + + ### Database Edit the file `config/db-local.php` with real data, for example: @@ -96,10 +114,7 @@ return [ ]; ``` -**NOTES:** -- Yii won't create the database for you, this has to be done manually before you can access it. -- Check and edit the other files in the `config/` directory to customize your application as required. -- Refer to the README in the `tests` directory for information specific to basic application tests. +Yii won't create the database for you, this has to be done manually before you can access it. From 5abb94357e5a522b685a6fe4b01f99e7a21aab42 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sun, 8 Jan 2017 13:42:15 +0100 Subject: [PATCH 3/8] Use `yii2-composer` to copying files. --- composer.json | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index f9ccafc1d..f4c3efea5 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "require": { "php": ">=5.4.0", "yiisoft/yii2": "~2.0.5", + "yiisoft/yii2-composer": "~2.0.5", "yiisoft/yii2-bootstrap": "~2.0.0", "yiisoft/yii2-swiftmailer": "~2.0.0" }, @@ -32,16 +33,15 @@ "process-timeout": 1800 }, "scripts": { - "post-install-cmd": [ - "php -r \"if(!file_exists('config/console-local.php')){copy('config/templates/console-local.php', 'config/console-local.php');}\"", - "php -r \"if(!file_exists('config/web-local.php')){copy('config/templates/web-local.php', 'config/web-local.php');}\"", - "php -r \"if(!file_exists('config/db-local.php')){copy('config/templates/db-local.php', 'config/db-local.php');}\"", - "php -r \"if(!file_exists('config/testdb-local.php')){copy('config/templates/testdb-local.php', 'config/testdb-local.php');}\"", - "php -r \"if(!file_exists('config/test-local.php')){copy('config/templates/test-local.php', 'config/test-local.php');}\"", - "yii\\composer\\Installer::postCreateProject" + "post-install-cmd": [ + "yii\\composer\\Installer::postInstall" + ], + "post-update-cmd": [ + "yii\\composer\\Installer::postInstall" ], "post-create-project-cmd": [ - "yii\\composer\\Installer::postCreateProject" + "yii\\composer\\Installer::postCreateProject", + "yii\\composer\\Installer::postInstall" ] }, "extra": { @@ -52,6 +52,17 @@ "web/assets": "0777", "yii": "0755" } + ] + }, + "yii\\composer\\Installer::postInstall": { + "copyFiles": [ + { + "config/templates/console-local.php": "config/console-local.php", + "config/templates/web-local.php": "config/web-local.php", + "config/templates/db-local.php": "config/db-local.php", + "config/templates/testdb-local.php": "config/testdb-local.php", + "config/templates/test-local.php": "config/test-local.php" + } ], "generateCookieValidationKey": [ "config/web-local.php" From e4ea5d89865f9cf4884b1648f216bdb241cb2c21 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sun, 8 Jan 2017 13:42:54 +0100 Subject: [PATCH 4/8] Improve travis config. --- .travis.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78b2bd25d..7f1600fd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,14 +19,6 @@ install: - travis_retry composer self-update && composer --version - travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1" - travis_retry composer update --dev --prefer-dist --no-interaction -# setup application: - - | - cp config/templates/console-local.php config/console-local.php - cp config/templates/web-local.php config/web-local.php - cp config/templates/db-local.php config/db-local.php - cp config/templates/test-local.php config/test-local.php - cp config/templates/testdb-local.php config/testdb-local.php - sed -i "s/'cookieValidationKey' => ''/'cookieValidationKey' => 'testkey'/" config/web-local.php script: - | From b3a07ce80aeb5fcdda830beb73f0797ffdd1adb2 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sun, 8 Jan 2017 13:44:28 +0100 Subject: [PATCH 5/8] Update composer-asset-plugin version in travis install script. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7f1600fd4..5e15e5a70 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ cache: install: - travis_retry composer self-update && composer --version - - travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1" + - travis_retry composer global require "fxp/composer-asset-plugin:^1.2.0" - travis_retry composer update --dev --prefer-dist --no-interaction script: From e3eeef1fa0512ed2aa8298d0329fc1f4df3200cd Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sun, 8 Jan 2017 13:47:52 +0100 Subject: [PATCH 6/8] Add PHP 7.1 to travis test. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 5e15e5a70..95312d2a0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ php: - 5.5 - 5.6 - 7.0 + - 7.1 # - hhvm # faster builds on new travis setup not using sudo From d7f1b7e81003c95f31968be1d17f552b01f9618d Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sun, 8 Jan 2017 14:14:05 +0100 Subject: [PATCH 7/8] Improve README.md formatting [skip ci] --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2ea1dfe0f..f14125cdf 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ at [getcomposer.org](http://getcomposer.org/doc/00-intro.md#installation-nix). You can then install this project template using the following command: -~~~ +~~~shell php composer.phar global require "fxp/composer-asset-plugin:^1.2.0" php composer.phar create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic ~~~ @@ -130,7 +130,7 @@ By default there are 3 test suites: Tests can be executed by running -``` +```shell composer exec codecept run ``` @@ -150,19 +150,19 @@ To execute acceptance tests do the following: 3. Update dependencies with Composer - ``` + ```shell composer update ``` 4. Download [Selenium Server](http://www.seleniumhq.org/download/) and launch it: - ``` + ```shell java -jar ~/selenium-server-standalone-x.xx.x.jar ``` 5. (Optional) Create `yii2_basic_tests` database and update it by applying migrations if you have them. - ``` + ```shell tests/bin/yii migrate ``` @@ -171,13 +171,13 @@ To execute acceptance tests do the following: 6. Start web server: - ``` + ```shell tests/bin/yii serve ``` 7. Now you can run all available tests - ``` + ```shell # run all available tests composer exec codecept run @@ -193,14 +193,14 @@ To execute acceptance tests do the following: By default, code coverage is disabled in `codeception.yml` configuration file, you should uncomment needed rows to be able to collect code coverage. You can run your tests and collect coverage with the following command: -``` -#collect coverage for all tests +```shell +# collect coverage for all tests composer exec codecept run -- --coverage-html --coverage-xml -#collect coverage only for unit tests +# collect coverage only for unit tests composer exec codecept run unit -- --coverage-html --coverage-xml -#collect coverage for unit and functional tests +# collect coverage for unit and functional tests composer exec codecept run functional,unit -- --coverage-html --coverage-xml ``` From ddd290043b3dee26b534f005fe5d9c55120cc3f0 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Thu, 2 Feb 2017 23:27:14 +0100 Subject: [PATCH 8/8] Do not call `Installer::postInstall()` for `post-create-project-cmd`. --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index f4c3efea5..107e811ef 100644 --- a/composer.json +++ b/composer.json @@ -40,8 +40,7 @@ "yii\\composer\\Installer::postInstall" ], "post-create-project-cmd": [ - "yii\\composer\\Installer::postCreateProject", - "yii\\composer\\Installer::postInstall" + "yii\\composer\\Installer::postCreateProject" ] }, "extra": {