diff --git a/composer.json b/composer.json index 14ad2486..e5e60d73 100644 --- a/composer.json +++ b/composer.json @@ -33,8 +33,8 @@ }, "scripts": { "post-install-cmd": [ - "php misuzu_migrate.php", - "php misuzu_setup.php" + "php misuzu.php migrate", + "php misuzu.php cron" ] }, "config": { diff --git a/composer.lock b/composer.lock index f214bbcc..85df22a7 100644 --- a/composer.lock +++ b/composer.lock @@ -375,16 +375,16 @@ }, { "name": "nesbot/carbon", - "version": "1.27.0", + "version": "1.32.0", "source": { "type": "git", "url": "https://github.com/briannesbitt/Carbon.git", - "reference": "ef81c39b67200dcd7401c24363dcac05ac3a4fe9" + "reference": "64563e2b9f69e4db1b82a60e81efa327a30ff343" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/ef81c39b67200dcd7401c24363dcac05ac3a4fe9", - "reference": "ef81c39b67200dcd7401c24363dcac05ac3a4fe9", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/64563e2b9f69e4db1b82a60e81efa327a30ff343", + "reference": "64563e2b9f69e4db1b82a60e81efa327a30ff343", "shasum": "" }, "require": { @@ -396,6 +396,13 @@ "phpunit/phpunit": "^4.8.35 || ^5.7" }, "type": "library", + "extra": { + "laravel": { + "providers": [ + "Carbon\\Laravel\\ServiceProvider" + ] + } + }, "autoload": { "psr-4": { "": "src/" @@ -419,20 +426,20 @@ "datetime", "time" ], - "time": "2018-04-23T09:02:57+00:00" + "time": "2018-07-05T06:59:26+00:00" }, { "name": "swiftmailer/swiftmailer", - "version": "v6.0.2", + "version": "v6.1.1", "source": { "type": "git", "url": "https://github.com/swiftmailer/swiftmailer.git", - "reference": "412333372fb6c8ffb65496a2bbd7321af75733fc" + "reference": "aa899fef280b1c1aec8d5d4ac069af7f80c89a23" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/412333372fb6c8ffb65496a2bbd7321af75733fc", - "reference": "412333372fb6c8ffb65496a2bbd7321af75733fc", + "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/aa899fef280b1c1aec8d5d4ac069af7f80c89a23", + "reference": "aa899fef280b1c1aec8d5d4ac069af7f80c89a23", "shasum": "" }, "require": { @@ -443,10 +450,14 @@ "mockery/mockery": "~0.9.1", "symfony/phpunit-bridge": "~3.3@dev" }, + "suggest": { + "ext-intl": "Needed to support internationalized email addresses", + "true/punycode": "Needed to support internationalized email addresses, if ext-intl is not installed" + }, "type": "library", "extra": { "branch-alias": { - "dev-master": "6.0-dev" + "dev-master": "6.1-dev" } }, "autoload": { @@ -468,13 +479,13 @@ } ], "description": "Swiftmailer, free feature-rich PHP mailer", - "homepage": "http://swiftmailer.symfony.com", + "homepage": "https://swiftmailer.symfony.com", "keywords": [ "email", "mail", "mailer" ], - "time": "2017-09-30T22:39:41+00:00" + "time": "2018-07-04T11:12:44+00:00" }, { "name": "symfony/polyfill-mbstring", @@ -537,16 +548,16 @@ }, { "name": "symfony/translation", - "version": "v4.0.9", + "version": "v4.1.1", "source": { "type": "git", "url": "https://github.com/symfony/translation.git", - "reference": "ad3abf08eb3450491d8d76513100ef58194cd13e" + "reference": "b6d8164085ee0b6debcd1b7a131fd6f63bb04854" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/translation/zipball/ad3abf08eb3450491d8d76513100ef58194cd13e", - "reference": "ad3abf08eb3450491d8d76513100ef58194cd13e", + "url": "https://api.github.com/repos/symfony/translation/zipball/b6d8164085ee0b6debcd1b7a131fd6f63bb04854", + "reference": "b6d8164085ee0b6debcd1b7a131fd6f63bb04854", "shasum": "" }, "require": { @@ -561,6 +572,7 @@ "require-dev": { "psr/log": "~1.0", "symfony/config": "~3.4|~4.0", + "symfony/console": "~3.4|~4.0", "symfony/dependency-injection": "~3.4|~4.0", "symfony/finder": "~2.8|~3.0|~4.0", "symfony/intl": "~3.4|~4.0", @@ -574,7 +586,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.0-dev" + "dev-master": "4.1-dev" } }, "autoload": { @@ -601,7 +613,7 @@ ], "description": "Symfony Translation Component", "homepage": "https://symfony.com", - "time": "2018-04-30T01:23:47+00:00" + "time": "2018-06-22T08:59:39+00:00" }, { "name": "twig/extensions", @@ -783,25 +795,28 @@ }, { "name": "myclabs/deep-copy", - "version": "1.7.0", + "version": "1.8.1", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "3b8a3a99ba1f6a3952ac2747d989303cbd6b7a3e" + "reference": "3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3b8a3a99ba1f6a3952ac2747d989303cbd6b7a3e", - "reference": "3b8a3a99ba1f6a3952ac2747d989303cbd6b7a3e", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8", + "reference": "3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8", "shasum": "" }, "require": { - "php": "^5.6 || ^7.0" + "php": "^7.1" + }, + "replace": { + "myclabs/deep-copy": "self.version" }, "require-dev": { "doctrine/collections": "^1.0", "doctrine/common": "^2.6", - "phpunit/phpunit": "^4.1" + "phpunit/phpunit": "^7.1" }, "type": "library", "autoload": { @@ -824,7 +839,7 @@ "object", "object graph" ], - "time": "2017-10-19T19:58:43+00:00" + "time": "2018-06-11T23:09:50+00:00" }, { "name": "phar-io/manifest", @@ -1394,16 +1409,16 @@ }, { "name": "phpunit/phpunit", - "version": "6.5.8", + "version": "6.5.9", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "4f21a3c6b97c42952fd5c2837bb354ec0199b97b" + "reference": "093ca5508174cd8ab8efe44fd1dde447adfdec8f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/4f21a3c6b97c42952fd5c2837bb354ec0199b97b", - "reference": "4f21a3c6b97c42952fd5c2837bb354ec0199b97b", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/093ca5508174cd8ab8efe44fd1dde447adfdec8f", + "reference": "093ca5508174cd8ab8efe44fd1dde447adfdec8f", "shasum": "" }, "require": { @@ -1474,20 +1489,20 @@ "testing", "xunit" ], - "time": "2018-04-10T11:38:34+00:00" + "time": "2018-07-03T06:40:40+00:00" }, { "name": "phpunit/phpunit-mock-objects", - "version": "5.0.6", + "version": "5.0.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "33fd41a76e746b8fa96d00b49a23dadfa8334cdf" + "reference": "3eaf040f20154d27d6da59ca2c6e28ac8fd56dce" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/33fd41a76e746b8fa96d00b49a23dadfa8334cdf", - "reference": "33fd41a76e746b8fa96d00b49a23dadfa8334cdf", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/3eaf040f20154d27d6da59ca2c6e28ac8fd56dce", + "reference": "3eaf040f20154d27d6da59ca2c6e28ac8fd56dce", "shasum": "" }, "require": { @@ -1533,7 +1548,7 @@ "mock", "xunit" ], - "time": "2018-01-06T05:45:45+00:00" + "time": "2018-05-29T13:50:43+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", diff --git a/misuzu.php b/misuzu.php index 29b82eca..284fd87a 100644 --- a/misuzu.php +++ b/misuzu.php @@ -30,7 +30,102 @@ $app = new Application( ); $app->startDatabase(); -if (PHP_SAPI !== 'cli') { +if (PHP_SAPI === 'cli') { + if ($argv[0] === basename(__FILE__)) { + switch ($argv[1] ?? null) { + case 'cron': + $db = Database::connection(); + + // Ensure main role exists. + $db->query(" + INSERT IGNORE INTO `msz_roles` + (`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `created_at`) + VALUES + (1, 'Member', 1, 1073741824, NULL, NOW()) + "); + + // Ensures all users are in the main role. + $db->query(' + INSERT INTO `msz_user_roles` + (`user_id`, `role_id`) + SELECT `user_id`, 1 FROM `msz_users` as u + WHERE NOT EXISTS ( + SELECT 1 + FROM `msz_user_roles` as ur + WHERE `role_id` = 1 + AND u.`user_id` = ur.`user_id` + ) + '); + + // Ensures all display_role values are correct with `msz_user_roles` + $db->query(' + UPDATE `msz_users` as u + SET `display_role` = ( + SELECT ur.`role_id` + FROM `msz_user_roles` as ur + LEFT JOIN `msz_roles` as r + ON r.`role_id` = ur.`role_id` + WHERE ur.`user_id` = u.`user_id` + ORDER BY `role_hierarchy` DESC + LIMIT 1 + ) + WHERE NOT EXISTS ( + SELECT 1 + FROM `msz_user_roles` as ur + WHERE ur.`role_id` = u.`display_role` + AND `ur`.`user_id` = u.`user_id` + ) + '); + break; + + case 'migrate': + $migrationTargets = [ + 'mysql-main' => __DIR__ . '/database', + ]; + $doRollback = !empty($argv[2]) && $argv[2] === 'rollback'; + $targetDb = isset($argv[$doRollback ? 3 : 2]) ? $argv[$doRollback ? 3 : 2] : null; + + if ($targetDb !== null && !array_key_exists($targetDb, $migrationTargets)) { + echo 'Invalid target database connection.' . PHP_EOL; + break; + } + + foreach ($migrationTargets as $db => $path) { + echo "Creating migration manager for '{$db}'..." . PHP_EOL; + $migrationManager = new DatabaseMigrationManager(Database::connection($db), $path); + $migrationManager->setLogger(function ($log) { + echo $log . PHP_EOL; + }); + + if ($doRollback) { + echo "Rolling back last migrations for '{$db}'..." . PHP_EOL; + $migrationManager->rollback(); + } else { + echo "Running migrations for '{$db}'..." . PHP_EOL; + $migrationManager->migrate(); + } + + $errors = $migrationManager->getErrors(); + $errorCount = count($errors); + + if ($errorCount < 1) { + echo 'Completed with no errors!' . PHP_EOL; + } else { + echo PHP_EOL . "There were {$errorCount} errors during the migrations..." . PHP_EOL; + + foreach ($errors as $error) { + echo $error . PHP_EOL; + } + } + } + break; + + default: + echo 'Unknown command.' . PHP_EOL; + break; + } + } +} else { $storage_dir = $app->getStoragePath(); if (!$storage_dir->isReadable() || !$storage_dir->isWritable()) { diff --git a/misuzu_migrate.php b/misuzu_migrate.php deleted file mode 100644 index 72e20d74..00000000 --- a/misuzu_migrate.php +++ /dev/null @@ -1,65 +0,0 @@ - __DIR__ . '/database', -]); - -function migrate_log($log): void -{ - echo $log . PHP_EOL; -} - -if (PHP_SAPI !== 'cli') { - migrate_log('This can only be run from a CLI, if you can access this from a web browser your configuration is bad.'); - exit; -} - -$migrateTargets = MSZ_MIGRATABLE_DATABASES; - -$doRollback = isset($argv[1]) && $argv[1] === 'rollback'; -$targetDb = isset($argv[$doRollback ? 2 : 1]) ? $argv[$doRollback ? 2 : 1] : null; - -if ($targetDb !== null) { - if (array_key_exists($targetDb, $migrateTargets)) { - $migrateTargets = [$targetDb => $migrateTargets[$targetDb]]; - } else { - migrate_log('Invalid target database connection.'); - exit; - } -} - - -foreach ($migrateTargets as $db => $path) { - migrate_log("Creating migration manager for '{$db}'..."); - $migrationManager = new DatabaseMigrationManager(Database::connection($db), $path); - $migrationManager->setLogger('migrate_log'); - - if ($doRollback) { - migrate_log("Rolling back last migrations for '{$db}'..."); - $migrationManager->rollback(); - } else { - migrate_log("Running migrations for '{$db}'..."); - $migrationManager->migrate(); - } - - $errors = $migrationManager->getErrors(); - $errorCount = count($errors); - - if ($errorCount < 1) { - migrate_log('Completed with no errors!'); - } else { - migrate_log(PHP_EOL . "There were {$errorCount} errors during the migrations..."); - - foreach ($errors as $error) { - migrate_log($error); - } - } -} diff --git a/misuzu_setup.php b/misuzu_setup.php deleted file mode 100644 index 2f885185..00000000 --- a/misuzu_setup.php +++ /dev/null @@ -1,65 +0,0 @@ -query(' - SELECT `role_id` - FROM `msz_roles` - WHERE `role_id` = 1 -')->fetchColumn(); - -if ($mainRoleId !== 1) { - $db->query(" - REPLACE INTO `msz_roles` - (`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `created_at`) - VALUES - (1, 'Member', 1, 1073741824, NULL, NOW()) - "); -} - -// Ensures all users are in the main role. -$db->query(' - INSERT INTO `msz_user_roles` - (`user_id`, `role_id`) - SELECT `user_id`, 1 FROM `msz_users` as u - WHERE NOT EXISTS ( - SELECT 1 - FROM `msz_user_roles` as ur - WHERE `role_id` = 1 - AND u.`user_id` = ur.`user_id` - ) -'); - -// Ensures all display_role values are correct with `msz_user_roles` -$db->query(' - UPDATE `msz_users` as u - SET `display_role` = ( - SELECT ur.`role_id` - FROM `msz_user_roles` as ur - LEFT JOIN `msz_roles` as r - ON r.`role_id` = ur.`role_id` - WHERE ur.`user_id` = u.`user_id` - ORDER BY `role_hierarchy` DESC - LIMIT 1 - ) - WHERE NOT EXISTS ( - SELECT 1 - FROM `msz_user_roles` as ur - WHERE ur.`role_id` = u.`display_role` - AND `ur`.`user_id` = u.`user_id` - ) -');