From d98f03e2e5d5ee084e755a9ce52990c9fa71c2a5 Mon Sep 17 00:00:00 2001 From: Fadil Date: Sat, 20 Aug 2022 22:55:35 +0400 Subject: [PATCH] added phinx for database & remove in-built migration/seeder --- README.md | 37 +- composer.json | 3 +- composer.lock | 590 +++++++++++++++++- phinx.php | 53 ++ .../20220820174815_users_migration.php | 39 ++ phinx/seeds/UserSeeder.php | 38 ++ src/Commands/UserCommand.php | 56 +- src/Controller/UserController.php | 3 +- src/Repository/UsersRepository.php | 44 +- 9 files changed, 775 insertions(+), 88 deletions(-) create mode 100644 phinx.php create mode 100644 phinx/migrations/20220820174815_users_migration.php create mode 100644 phinx/seeds/UserSeeder.php diff --git a/README.md b/README.md index 84143d2..9d6a104 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# HFX v4 +# PHP framework with Phinx ![Maintainer](https://img.shields.io/badge/maintainer-fadilxcoder-blue) [![Windows](https://svgshare.com/i/ZhY.svg)](https://svgshare.com/i/ZhY.svg) @@ -24,6 +24,19 @@ - Uses node - webpack - compilation of assets - `public/dist` for compiled assets +--- + +## Phinx + +- Create a migration : `vendor/bin/phinx create UsersMigration` +- Add SQL to created migration +- Migrate data : `vendor/bin/phinx migrate` +- Create newly created seeding : `vendor/bin/phinx seed:create UserSeeder` +- Seed data : `vendor/bin/phinx seed:run` +- `vendor/bin/phinx migrate -t 20220820174815 & vendor/bin/phinx seed:run -s UserSeeder` - Specific migration / seeder + +--- + ### Routing notes - Documentation on packagist & https://github.com/nikic/FastRoute @@ -78,6 +91,8 @@ `'/[{extra}]'` : Allowing `/?fb_click=FB_ID&utm=UTM_ID>m=GTM_ID` & `/` +--- + ### Routing Regex - The rules : `'/content/{id:\d+}/{slug:[a-z]+}[/{extra}]'` @@ -93,7 +108,9 @@ - - `&` *ampersand* - Finally : `{slug:[a-zA-Z0-9-_&]+}` -## Packages (require / require-dev) +--- + +## Packages (require / require-dev) / URLs - https://packagist.org/packages/nikic/fast-route - `Routes` - https://packagist.org/packages/symfony/http-kernel @@ -106,12 +123,16 @@ - https://packagist.org/packages/filp/whoops - `Debugger` - https://packagist.org/packages/fakerphp/faker // https://fakerphp.github.io/ - `Dummy data provider` - https://packagist.org/packages/mnapoli/silly - `CLI` +- https://book.cakephp.org/phinx/0/en/contents.html - `Phinx Documentation` + +--- ## Development utilities - https://picocss.com/ `Minimal CSS Framework for semantic HTML` - https://picocss.com/examples/preview/ & https://github.com/picocss/examples `Pico examples` +--- ## Environment configuration @@ -124,6 +145,8 @@ ``` +--- + ## Usage - Set up `vhosts` @@ -131,8 +154,9 @@ - `composer install` - `composer update` when modifying specific **packages** - *PS. `packages/chrome/`, `"version": "master"` in `composer.json`* - DB tables : -- - `php bin/console database:users up && php bin/console database:users seed` - Create the table `hfx_users` and populate it -- - `php bin/console database:users down` - Drop the table `hfx_users` +- - `vendor/bin/phinx migrate` - Migrate DB +- - `vendor/bin/phinx seed:run` - Popupate DB +- Command : `php bin/console database:users 1 / php bin/console database:users 155` - Fetching data from `users` with specified ID - Assets `npm install` - `npm rebuild node-sass` - Changing environment - Compiling assets @@ -140,7 +164,10 @@ - - `npm run css-build` : compile CSS - - `npm run js-build-watch` : compile JS in *watch mode* -## Demo URL +--- + +## Demo URL for testing + - - http://helifox.local/ - - http://helifox.local/?fb_click=FB_ID&utm=UTM_ID>m=GTM_ID - - http://helifox.local/content/5/fx/?action=cancel diff --git a/composer.json b/composer.json index 04069b7..83f98ca 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "nikic/fast-route": "^1.3", "mnapoli/silly": "^1.7", "ghost-dvlpr/chrome-php": "dev-master", - "fakerphp/faker": "^1.19" + "fakerphp/faker": "^1.19", + "robmorgan/phinx": "^0.12.12" }, "require-dev": { "filp/whoops": "^2.13" diff --git a/composer.lock b/composer.lock index de690c9..9d4a09e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,237 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6ef78493017a9f863a86e3ba0c26bb81", + "content-hash": "50394a8a646242c98da8f734e1f5a5ac", "packages": [ + { + "name": "cakephp/core", + "version": "4.4.4", + "source": { + "type": "git", + "url": "https://github.com/cakephp/core.git", + "reference": "884555efcf573ea23597f9c88a12ed6d58af00f3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cakephp/core/zipball/884555efcf573ea23597f9c88a12ed6d58af00f3", + "reference": "884555efcf573ea23597f9c88a12ed6d58af00f3", + "shasum": "" + }, + "require": { + "cakephp/utility": "^4.0", + "php": ">=7.4.0" + }, + "suggest": { + "cakephp/cache": "To use Configure::store() and restore().", + "cakephp/event": "To use PluginApplicationInterface or plugin applications.", + "league/container": "To use Container and ServiceProvider classes" + }, + "type": "library", + "autoload": { + "files": [ + "functions.php" + ], + "psr-4": { + "Cake\\Core\\": "." + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "CakePHP Community", + "homepage": "https://github.com/cakephp/core/graphs/contributors" + } + ], + "description": "CakePHP Framework Core classes", + "homepage": "https://cakephp.org", + "keywords": [ + "cakephp", + "core", + "framework" + ], + "support": { + "forum": "https://stackoverflow.com/tags/cakephp", + "irc": "irc://irc.freenode.org/cakephp", + "issues": "https://github.com/cakephp/cakephp/issues", + "source": "https://github.com/cakephp/core" + }, + "time": "2022-05-31T20:57:08+00:00" + }, + { + "name": "cakephp/database", + "version": "4.4.4", + "source": { + "type": "git", + "url": "https://github.com/cakephp/database.git", + "reference": "25694dd69cbfa61a6b17674b76aa2accd1f779f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cakephp/database/zipball/25694dd69cbfa61a6b17674b76aa2accd1f779f9", + "reference": "25694dd69cbfa61a6b17674b76aa2accd1f779f9", + "shasum": "" + }, + "require": { + "cakephp/core": "^4.0", + "cakephp/datasource": "^4.0", + "php": ">=7.4.0" + }, + "suggest": { + "cakephp/i18n": "If you are using locale-aware datetime formats or Chronos types." + }, + "type": "library", + "autoload": { + "psr-4": { + "Cake\\Database\\": "." + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "CakePHP Community", + "homepage": "https://github.com/cakephp/database/graphs/contributors" + } + ], + "description": "Flexible and powerful Database abstraction library with a familiar PDO-like API", + "homepage": "https://cakephp.org", + "keywords": [ + "abstraction", + "cakephp", + "database", + "database abstraction", + "pdo" + ], + "support": { + "forum": "https://stackoverflow.com/tags/cakephp", + "irc": "irc://irc.freenode.org/cakephp", + "issues": "https://github.com/cakephp/cakephp/issues", + "source": "https://github.com/cakephp/database" + }, + "time": "2022-07-21T13:36:55+00:00" + }, + { + "name": "cakephp/datasource", + "version": "4.4.4", + "source": { + "type": "git", + "url": "https://github.com/cakephp/datasource.git", + "reference": "f0dd560edbaf6dac8045e7804c25d2e8e6ef0d3c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cakephp/datasource/zipball/f0dd560edbaf6dac8045e7804c25d2e8e6ef0d3c", + "reference": "f0dd560edbaf6dac8045e7804c25d2e8e6ef0d3c", + "shasum": "" + }, + "require": { + "cakephp/core": "^4.0", + "php": ">=7.4.0", + "psr/log": "^1.0 || ^2.0", + "psr/simple-cache": "^1.0 || ^2.0" + }, + "suggest": { + "cakephp/cache": "If you decide to use Query caching.", + "cakephp/collection": "If you decide to use ResultSetInterface.", + "cakephp/utility": "If you decide to use EntityTrait." + }, + "type": "library", + "autoload": { + "psr-4": { + "Cake\\Datasource\\": "." + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "CakePHP Community", + "homepage": "https://github.com/cakephp/datasource/graphs/contributors" + } + ], + "description": "Provides connection managing and traits for Entities and Queries that can be reused for different datastores", + "homepage": "https://cakephp.org", + "keywords": [ + "cakephp", + "connection management", + "datasource", + "entity", + "query" + ], + "support": { + "forum": "https://stackoverflow.com/tags/cakephp", + "irc": "irc://irc.freenode.org/cakephp", + "issues": "https://github.com/cakephp/cakephp/issues", + "source": "https://github.com/cakephp/datasource" + }, + "time": "2022-07-28T08:59:25+00:00" + }, + { + "name": "cakephp/utility", + "version": "4.4.4", + "source": { + "type": "git", + "url": "https://github.com/cakephp/utility.git", + "reference": "32866aac3d4fef7649fb524da6c59e14e7d18a0d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cakephp/utility/zipball/32866aac3d4fef7649fb524da6c59e14e7d18a0d", + "reference": "32866aac3d4fef7649fb524da6c59e14e7d18a0d", + "shasum": "" + }, + "require": { + "cakephp/core": "^4.0", + "php": ">=7.4.0" + }, + "suggest": { + "ext-intl": "To use Text::transliterate() or Text::slug()", + "lib-ICU": "To use Text::transliterate() or Text::slug()" + }, + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Cake\\Utility\\": "." + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "CakePHP Community", + "homepage": "https://github.com/cakephp/utility/graphs/contributors" + } + ], + "description": "CakePHP Utility classes such as Inflector, String, Hash, and Security", + "homepage": "https://cakephp.org", + "keywords": [ + "cakephp", + "hash", + "inflector", + "security", + "string", + "utility" + ], + "support": { + "forum": "https://stackoverflow.com/tags/cakephp", + "irc": "irc://irc.freenode.org/cakephp", + "issues": "https://github.com/cakephp/cakephp/issues", + "source": "https://github.com/cakephp/utility" + }, + "time": "2022-07-16T21:24:52+00:00" + }, { "name": "fakerphp/faker", "version": "v1.19.0", @@ -728,6 +957,143 @@ }, "time": "2021-05-03T11:20:27+00:00" }, + { + "name": "psr/simple-cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "support": { + "source": "https://github.com/php-fig/simple-cache/tree/master" + }, + "time": "2017-10-23T01:57:42+00:00" + }, + { + "name": "robmorgan/phinx", + "version": "0.12.12", + "source": { + "type": "git", + "url": "https://github.com/cakephp/phinx.git", + "reference": "9a6ce1e7fdf0fa4e602ba5875b5bc9442ccaa115" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cakephp/phinx/zipball/9a6ce1e7fdf0fa4e602ba5875b5bc9442ccaa115", + "reference": "9a6ce1e7fdf0fa4e602ba5875b5bc9442ccaa115", + "shasum": "" + }, + "require": { + "cakephp/database": "^4.0", + "php": ">=7.2", + "psr/container": "^1.0 || ^2.0", + "symfony/config": "^3.4|^4.0|^5.0|^6.0", + "symfony/console": "^3.4|^4.0|^5.0|^6.0" + }, + "require-dev": { + "cakephp/cakephp-codesniffer": "^4.0", + "ext-json": "*", + "ext-pdo": "*", + "phpunit/phpunit": "^8.5|^9.3", + "sebastian/comparator": ">=1.2.3", + "symfony/yaml": "^3.4|^4.0|^5.0" + }, + "suggest": { + "ext-json": "Install if using JSON configuration format", + "ext-pdo": "PDO extension is needed", + "symfony/yaml": "Install if using YAML configuration format" + }, + "bin": [ + "bin/phinx" + ], + "type": "library", + "autoload": { + "psr-4": { + "Phinx\\": "src/Phinx/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Rob Morgan", + "email": "robbym@gmail.com", + "homepage": "https://robmorgan.id.au", + "role": "Lead Developer" + }, + { + "name": "Woody Gilk", + "email": "woody.gilk@gmail.com", + "homepage": "https://shadowhand.me", + "role": "Developer" + }, + { + "name": "Richard Quadling", + "email": "rquadling@gmail.com", + "role": "Developer" + }, + { + "name": "CakePHP Community", + "homepage": "https://github.com/cakephp/phinx/graphs/contributors", + "role": "Developer" + } + ], + "description": "Phinx makes it ridiculously easy to manage the database migrations for your PHP app.", + "homepage": "https://phinx.org", + "keywords": [ + "database", + "database migrations", + "db", + "migrations", + "phinx" + ], + "support": { + "issues": "https://github.com/cakephp/phinx/issues", + "source": "https://github.com/cakephp/phinx/tree/0.12.12" + }, + "time": "2022-07-09T18:53:51+00:00" + }, { "name": "symfony/asset", "version": "v5.4.7", @@ -802,6 +1168,85 @@ ], "time": "2022-03-18T16:00:30+00:00" }, + { + "name": "symfony/config", + "version": "v5.4.11", + "source": { + "type": "git", + "url": "https://github.com/symfony/config.git", + "reference": "ec79e03125c1d2477e43dde8528535d90cc78379" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/config/zipball/ec79e03125c1d2477e43dde8528535d90cc78379", + "reference": "ec79e03125c1d2477e43dde8528535d90cc78379", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/filesystem": "^4.4|^5.0|^6.0", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-php80": "^1.16", + "symfony/polyfill-php81": "^1.22" + }, + "conflict": { + "symfony/finder": "<4.4" + }, + "require-dev": { + "symfony/event-dispatcher": "^4.4|^5.0|^6.0", + "symfony/finder": "^4.4|^5.0|^6.0", + "symfony/messenger": "^4.4|^5.0|^6.0", + "symfony/service-contracts": "^1.1|^2|^3", + "symfony/yaml": "^4.4|^5.0|^6.0" + }, + "suggest": { + "symfony/yaml": "To use the yaml reference dumper" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Config\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Helps you find, load, combine, autofill and validate configuration values of any kind", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/config/tree/v5.4.11" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-07-20T13:00:38+00:00" + }, { "name": "symfony/console", "version": "v5.4.9", @@ -1203,6 +1648,70 @@ ], "time": "2022-01-02T09:53:40+00:00" }, + { + "name": "symfony/filesystem", + "version": "v5.4.11", + "source": { + "type": "git", + "url": "https://github.com/symfony/filesystem.git", + "reference": "6699fb0228d1bc35b12aed6dd5e7455457609ddd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/6699fb0228d1bc35b12aed6dd5e7455457609ddd", + "reference": "6699fb0228d1bc35b12aed6dd5e7455457609ddd", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.8", + "symfony/polyfill-php80": "^1.16" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Filesystem\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides basic utilities for the filesystem", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/filesystem/tree/v5.4.11" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-07-20T13:00:38+00:00" + }, { "name": "symfony/http-foundation", "version": "v5.4.9", @@ -1880,6 +2389,85 @@ ], "time": "2022-05-10T07:21:04+00:00" }, + { + "name": "symfony/polyfill-php81", + "version": "v1.26.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php81.git", + "reference": "13f6d1271c663dc5ae9fb843a8f16521db7687a1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php81/zipball/13f6d1271c663dc5ae9fb843a8f16521db7687a1", + "reference": "13f6d1271c663dc5ae9fb843a8f16521db7687a1", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.26-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Php81\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php81/tree/v1.26.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-05-24T11:49:31+00:00" + }, { "name": "symfony/service-contracts", "version": "v2.5.1", diff --git a/phinx.php b/phinx.php new file mode 100644 index 0000000..f3e4894 --- /dev/null +++ b/phinx.php @@ -0,0 +1,53 @@ +load(); + +return +[ + 'paths' => [ + 'migrations' => '%%PHINX_CONFIG_DIR%%/phinx/migrations', + 'seeds' => '%%PHINX_CONFIG_DIR%%/phinx/seeds' + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_environment' => 'development', + 'production' => [ + 'adapter' => 'mysql', + 'host' => $_ENV['DB_HOST'], + 'name' => $_ENV['DB_NAME'], + 'user' => $_ENV['DB_USERNAME'], + 'pass' => $_ENV['DB_PASSWORD'], + 'port' => '3306', + 'charset' => 'utf8', + ], + 'development' => [ + 'adapter' => 'mysql', + 'host' => $_ENV['DB_HOST'], + 'name' => $_ENV['DB_NAME'], + 'user' => $_ENV['DB_USERNAME'], + 'pass' => $_ENV['DB_PASSWORD'], + 'port' => '3306', + 'charset' => 'utf8', + ], + 'testing' => [ + 'adapter' => 'mysql', + 'host' => $_ENV['DB_HOST'], + 'name' => $_ENV['DB_NAME'], + 'user' => $_ENV['DB_USERNAME'], + 'pass' => $_ENV['DB_PASSWORD'], + 'port' => '3306', + 'charset' => 'utf8', + ] + ], + 'version_order' => 'creation' +]; diff --git a/phinx/migrations/20220820174815_users_migration.php b/phinx/migrations/20220820174815_users_migration.php new file mode 100644 index 0000000..508e01b --- /dev/null +++ b/phinx/migrations/20220820174815_users_migration.php @@ -0,0 +1,39 @@ +execute($sql); + } + + /** + * Migrate Down. + */ + public function down() + { + $sql = ' + DROP TABLE IF EXISTS `hfx_users`; + '; + + $this->execute($sql); + } +} diff --git a/phinx/seeds/UserSeeder.php b/phinx/seeds/UserSeeder.php new file mode 100644 index 0000000..1f723d3 --- /dev/null +++ b/phinx/seeds/UserSeeder.php @@ -0,0 +1,38 @@ + $faker->uuid, + 'username' => $faker->companyEmail, + 'name' => $faker->name, + 'password' => 'c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec', # md5("admin") + 'last_login' => $faker->date($format = 'Y-m-d', $max = 'now'), + ]; + endfor; + + $users = $this->table('users'); + $users->truncate(); + $users + ->insert($data) + ->saveData() + ; + } +} diff --git a/src/Commands/UserCommand.php b/src/Commands/UserCommand.php index bf3eb1c..b9a0563 100644 --- a/src/Commands/UserCommand.php +++ b/src/Commands/UserCommand.php @@ -2,64 +2,40 @@ namespace App\Commands; -use App\Services\UserService; -use Faker\Factory as Factory; use App\Repository\UsersRepository; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class UserCommand { - private $usersRepository, $userService; + private $usersRepository; public function __construct( - UsersRepository $usersRepository, - UserService $userService + UsersRepository $usersRepository ) { $this->usersRepository = $usersRepository; - $this->userService = $userService; } public function __invoke(InputInterface $input, OutputInterface $output) { - $action = $input->getArgument('value'); + $output->writeln("\nBeginning query execution... \n"); - switch ($action) - { - case 'up': - $this->usersRepository->createTable(); - $output->writeln('Table `hfx_users` created !'); - break; - case 'down': - $this->usersRepository->dropTable(); - $output->writeln('Table `hfx_users` deleted !'); - break; - case 'seed': - $this->populateTable(); - $output->writeln('`hfx_users` populated.'); - break; - default: - $output->writeln('Missing arguments !'); - break; + $value = $input->getArgument('value'); + + if(empty($value)) { + $output->writeln('Missing argument !'); + return; } - } - private function populateTable() - { - $faker = Factory::create(); + $result = $this->usersRepository->getUser($value); + + if(empty($result)) { + $output->writeln('Not found !'); + return; + } - $this->usersRepository->cleanTable(); + dump($result); - for ($i=0; $i<25; $i++) : - $this->usersRepository->insertUser( - [ - "uuid" => $faker->uuid, - "username" => $faker->companyEmail, - "name" => $faker->name, - "password" => $this->userService->encryptUserPassword("admin"), - "last_login" => $faker->date($format = 'Y-m-d', $max = 'now'), - ] - ); - endfor; + $output->writeln("\nQuery executed ! \n"); } } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 5351696..d635205 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -30,12 +30,13 @@ public function displayUser(UsersRepository $usersRepository, User $user) ]); } - public function displayUsers(UsersRepository $usersRepository, User $user) + public function displayUsers(UsersRepository $usersRepository) { $results = (array) $usersRepository->getUsers(); $ai = new ArrayIterator(); foreach ($results as $result) { + $user = new User(); $user->setId($result->id_user); $user->setUuid($result->uuid); $user->setUsername($result->username); diff --git a/src/Repository/UsersRepository.php b/src/Repository/UsersRepository.php index 21c4203..9392c4d 100644 --- a/src/Repository/UsersRepository.php +++ b/src/Repository/UsersRepository.php @@ -12,7 +12,7 @@ public function getUsers() SELECT u.* FROM - `hfx_users` u + `users` u WHERE u.uuid LIKE :value ORDER BY @@ -30,7 +30,7 @@ public function getUser($id) SELECT u.* FROM - `hfx_users` u + `users` u WHERE u.id_user = :id '; @@ -44,7 +44,7 @@ public function updateUser($user) { $query = ' UPDATE - `hfx_users` + `users` SET last_login = :last_login WHERE @@ -61,7 +61,7 @@ public function insertUser(array $user) { $query = ' INSERT INTO - `hfx_users` ( + `users` ( uuid, username, name, @@ -86,40 +86,4 @@ public function insertUser(array $user) 'last_login' => $user['last_login'], ]); } - - public function createTable() - { - $query = ' - DROP TABLE IF EXISTS `hfx_users`; - CREATE TABLE `hfx_users` ( - `id_user` int(11) NOT NULL AUTO_INCREMENT, - `uuid` varchar(255) NOT NULL, - `username` varchar(45) DEFAULT NULL, - `name` varchar(255) DEFAULT NULL, - `password` varchar(255) DEFAULT NULL, - `last_login` datetime DEFAULT NULL, - PRIMARY KEY (`id_user`) - ) ENGINE=MyISAM DEFAULT CHARSET=latin1; - '; - - $this->db->exec($query); - } - - public function cleanTable() - { - $query = ' - TRUNCATE `hfx_users`; - '; - - $this->db->exec($query); - } - - public function dropTable() - { - $query = ' - DROP TABLE IF EXISTS `hfx_users`; - '; - - $this->db->exec($query); - } } \ No newline at end of file