Files
old-new-wiaas/README.md

246 lines
9.0 KiB
Markdown
Raw Permalink Normal View History

2018-07-30 08:54:41 +02:00
# New Wiaas
2018-06-14 16:49:28 +02:00
2018-07-30 08:54:41 +02:00
Wiaas implementation based on wordpress [woocommerce](https://woocommerce.com/) plugin.
2018-06-20 12:22:52 +02:00
2018-07-30 08:54:41 +02:00
## Dependencies
2018-06-20 12:22:52 +02:00
2018-07-30 08:54:41 +02:00
- PHP (7.0)
- MySQL server (5.7)
- [Composer](http://getcomposer.org)
- [WP CLI](https://wp-cli.org/#installing)
2018-08-06 17:36:57 +02:00
- [PHPUnit](https://phpunit.de/getting-started/phpunit-6.html) for Unit testing
2018-07-30 08:54:41 +02:00
- NodeJS (for building & developing frontend)
2018-06-20 12:49:07 +02:00
2018-07-30 08:54:41 +02:00
## Local Docker setup
2018-06-20 12:22:52 +02:00
2018-07-30 09:12:07 +02:00
1) Copy environment.env file and rename it to .env
2018-07-30 08:54:41 +02:00
2018-07-30 09:12:07 +02:00
cp environment.env .env
2) Edit .env file and add needed information
* `MYSQL_ROOT_PASSWORD` - MySQL database root password for MySQL docker image
* `MYSQL_DATABASE` - Wordpress database name
* `MYSQL_USER` - Wordpress database user
* `MYSQL_PASSWORD` - Wordpress database password
* `API_URL` - Full URL to WordPress home (http://localhost:8081 if you are testing locally)
2018-07-30 17:26:55 +02:00
* `WP_ENV` - Set to environment (`development`, `staging`, `production`)
2018-07-30 09:12:07 +02:00
* `WP_AUTH_KEY`, `WP_SECURE_AUTH_KEY`, `WP_LOGGED_IN_KEY`, `WP_NONCE_KEY`, `WP_AUTH_SALT`, `WP_SECURE_AUTH_SALT`, `WP_LOGGED_IN_SALT`, `WP_NONCE_SALT`, `WP_JWT_AUTH_SECRET_KEY`
2018-07-30 08:54:41 +02:00
2018-07-30 09:12:07 +02:00
3) Execute in the root of the project :
sudo docker-compose build
sudo docker-compose up
2018-06-20 12:22:52 +02:00
2018-07-30 09:12:07 +02:00
Frontend is running on http://localhost:8080 and backend on http://localhost:8081
2018-06-20 12:22:52 +02:00
2018-07-30 08:54:41 +02:00
## Local development setup
2018-06-20 12:22:52 +02:00
2018-07-30 08:54:41 +02:00
### DB setup
2018-06-14 16:49:28 +02:00
2018-07-30 09:12:07 +02:00
1) Create MYSQL database / user
2018-06-14 16:49:28 +02:00
2018-07-30 09:12:07 +02:00
CREATE DATABASE ${MYSQL_DATABASE};
CREATE USER '${MYSQL_USER}'@'localhost' IDENTIFIED BY '${MYSQL_PASSWORD}';
GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@localhost;
2018-07-09 10:45:48 +02:00
2018-07-30 09:12:07 +02:00
2) Seed database with `new-wiaas/database/clean-dump.sql`
2018-07-30 08:54:41 +02:00
### Backend setup
2018-07-30 09:12:07 +02:00
1) Environment variables
2018-07-30 08:54:41 +02:00
2018-07-30 17:26:55 +02:00
For handling wordpress environment variables during local development we use [dotenv](https://github.com/vlucas/phpdotenv) library
which enables loading variables from `.env` file.
If you wish to keep your docker and development setup different then generate `development.env` file which will then
be loaded by dotenv insted of default `.env` file.
cp environment.env development.env
2018-07-30 08:54:41 +02:00
2018-07-30 17:26:55 +02:00
Update `development.env` with your local environment variables overrides.
2018-07-30 09:12:07 +02:00
2) Apache
2018-07-30 08:54:41 +02:00
2018-07-30 09:12:07 +02:00
Point your apache server to `new-wiaas/backend` by updating apache default host files to point to it.
(do not copy folder content to `/var/www/html`, since then dotenv will not be able to load `local.env` file since it is placed one folder above.)
2018-07-30 08:54:41 +02:00
2018-07-30 10:26:49 +02:00
Copy .htaccess from `/docker/php` to `backend`.
2018-07-30 09:12:07 +02:00
3) Wordpress
2018-07-30 08:54:41 +02:00
2018-07-30 09:12:07 +02:00
Run setup commands to install wordpress and required plugins with composer:
cd backend
composer install
composer update-db
2018-07-30 08:54:41 +02:00
### Frondend setup
2018-07-30 09:12:07 +02:00
1) Install dependencies
cd frontend
npm install
2) Start local server
2018-07-30 08:54:41 +02:00
2018-07-30 09:12:07 +02:00
cd frontend
npm start
2018-07-30 09:00:38 +02:00
2018-07-30 08:54:41 +02:00
## Contribution
### Backend
2018-07-30 10:52:12 +02:00
#### Project structure
2018-07-30 08:54:41 +02:00
2018-07-30 09:12:07 +02:00
Project structure is inspired with [Roots Bedrock structure](https://roots.io/bedrock/).
2018-07-30 08:54:41 +02:00
2018-07-30 09:00:38 +02:00
├── composer.json # → Manage versions of WordPress, plugins & dependencies
├── config # → WordPress configuration files
│ ├── application.php # → Primary WP config file (wp-config.php equivalent)
│ └── environments # → Environment specific configs
│ ├── development.php # → Development config
│ ├── local.php # → Local config
│ └── production.php # → Production config
├── vendor # → Composer packages (never edit)
├── app # → wp-content equivalent
│ ├── mu-plugins # → Must use plugins
2018-07-30 10:52:12 +02:00
│ └── plugins # → Plugins
│ ├── wiaas # → Our code goes here
2018-07-30 09:00:38 +02:00
│ ├── themes # → Themes
│ └── uploads # → Uploads
├── wp-config.php # → Required by WP (never edit)
├── index.php # → WordPress view bootstrapper
└── wp # → WordPress core (never edit)
2018-07-30 09:08:28 +02:00
This approach enables:
2018-07-30 10:13:34 +02:00
- Better folder structure
- wordpress core is in separate folder and is treated as dependency
- separate environment config file for non sensitive wordpress config (ex: SCRIPT_DEBUG, DISALLOW_FILE_MODS)
- dedicated plugin folder for dev team under version control management (wiaas)
2018-07-30 09:08:28 +02:00
- Dependency management with Composer (wordpress core and plugins)
- Environment variables with [Dotenv](https://github.com/vlucas/phpdotenv)
- Enhanced security (all sensitive information is accessed with environment variables)
- Secure passwords with [wp-password-bcrypt](https://github.com/roots/wp-password-bcrypt)
- Composer as task runner for automatic wordpress db updates
2018-07-30 08:54:41 +02:00
2018-07-30 10:52:12 +02:00
#### Dependencies management
2018-07-30 08:54:41 +02:00
2018-07-30 09:08:28 +02:00
Wordpress core (`"johnpbloch/wordpress"`) and wordpress plugins code are managed with `composer.json` file.
But using combination of [Composer scripts](https://getcomposer.org/doc/articles/scripts.md) and [WP CLI commands](https://developer.wordpress.org/cli/commands/)
we can automate database updates as well (mainly plugin activation and db updates after plugin version change);
2018-07-30 10:52:12 +02:00
##### Adding new plugin
2018-07-30 09:08:28 +02:00
For adding new plugin from wordpress official repo add `"wpackagist-plugin/{plugin_name}": "{plugin_version}"` to `composer.json`.
2018-07-30 10:52:12 +02:00
composer require wpackagist-plugin/{plugin_name} --version={plugin_version}
2018-07-30 09:08:28 +02:00
Then add package to `"activate-plugins"` composer script in `composer.json` file in order in which you wish them to be activated.
2018-07-30 10:52:12 +02:00
"scripts": {
"activate-plugins": [
...,
wp plugin activate woocommerce {plugin_name}
]
}
2018-07-30 09:08:28 +02:00
If plugin exposes wp cli command for database updates add it to `"update-db"` composer script in `composer.json` file
(ex: woocommerce plugin exposes `wp wc update` which will apply pending database plugins for woocommerce).
2018-07-30 10:52:12 +02:00
Make sure that wiaas always applies its changes last.
"scripts": {
"update-db": [
"wp core update-db",
"composer activate-plugins",
...
{add wp cli command here},
"wp wiaas update-db"
]
}
2018-07-30 09:08:28 +02:00
For updating wordpress core or plugins, update version for package
wordpress core database changes will be applied with `wp core update-db` cli command ).
After any of these actions run:
2018-07-30 08:54:41 +02:00
2018-07-30 10:52:12 +02:00
composer update # → Will download latest code and update lock file
2018-07-30 09:08:28 +02:00
composer update-db # → Activate plugins and calls all exposed wp cli commands for db updates
2018-07-30 10:52:12 +02:00
##### Removing existing plugin
2018-09-06 13:07:14 +02:00
1) Remove it from composer.json with:
2018-07-30 10:52:12 +02:00
2018-07-30 10:55:13 +02:00
composer remove wpackagist-plugin/{plugin_name}
2018-07-30 10:52:12 +02:00
2018-09-06 13:07:14 +02:00
2) Remove it from `"activate-plugins"` and `"update-db"` scripts.
2018-07-30 10:55:13 +02:00
3) Then run:
composer update
2018-07-30 10:52:12 +02:00
2018-07-30 09:08:28 +02:00
If plugin has database changes that we wish to be removed that is oneoff migration that can be done from
2018-07-30 10:52:12 +02:00
admin panel or using script:
wp plugin deactivate {plugin_name} && composer remove wpackagist-plugin/{plugin_name}
2018-07-30 09:08:28 +02:00
WP CLI is very powerful tool for writing automation scripts without need to track and copy mysql dumps.
2018-07-30 10:52:12 +02:00
But if need arises we can think about using tool for db migration scripts.
2018-07-30 08:54:41 +02:00
2018-07-30 10:52:12 +02:00
##### Wiaas plugin
2018-07-30 09:08:28 +02:00
Our code is placed inside `backend/app/plugins/wiaas` folder.
2018-07-30 10:52:12 +02:00
###### Wiaas plugin folder structure:
├── includes # → Location for src files
│ ├── cli # → CLI commands exposed to WP CLI
│ └── db-updates # → DB updates
│ ├── wiaas-db-updates-functions.php # → Contains functions for db updates
├── wiaas.php # → Entry point
2018-07-30 09:08:28 +02:00
This folder will be managed by git and commited to repository (check `backend/.gitignore`).
2018-07-30 10:52:12 +02:00
###### Wiaas plugin DB changes
Wiaas exposes WP CLI command which will execute pending db updates for wiaas:
wp wiaas update-db
2018-09-06 13:07:14 +02:00
If your feature requires some database updates that can be easily executed from wordpress code, then:
2018-07-30 10:56:19 +02:00
2018-07-30 10:57:08 +02:00
1) Place this update to `wiaas/includes/db-updates/wiaas-db-update-functions.php` as a function named `wiaas_db_update_{name}`
2018-07-30 10:56:46 +02:00
2) Add this function to `wiaas/includes/class-wiaas-db-update.php` with its timestamp.
2018-07-30 10:52:12 +02:00
2018-07-30 09:08:28 +02:00
This way after `composer update-db` is executed your database update will be applied.
2018-08-06 17:36:57 +02:00
#### Unit testing
1) Install [PHPUnit](https://phpunit.de/getting-started/phpunit-6.html)
wget -O phpunit https://phar.phpunit.de/phpunit-6.phar
cp phpunit /usr/local/bin/phpunit
sudo chmod +x /usr/local/bin/phpunit
2) Setup wiaas plugin unit testing
cd backend/app/plugins/wiaas
2018-08-06 19:33:00 +02:00
./tests/bin/setup.sh [db-root] [db-root-pass]
2018-08-06 17:36:57 +02:00
Script will install test environment in `/tmp/wiaas-backend-test`
3) Now you can run `phpunit` inside `wiaas` directory