Skip to content

Commit

Permalink
Added support for project-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Stark committed Jan 23, 2019
1 parent 10a883e commit 432ea2e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Craft Automigrate

A Composer plugin that runs `craft migrate/all` after `composer install`, **if Craft is installed**.
A Composer plugin that runs `craft migrate/all` and `craft project-config/sync` after `composer install`, **if Craft is installed**.

By setting the ENV var `DISABLE_CRAFT_AUTOMIGRATE=1` you disable the plugin.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Runs migrations automatically",
"keywords": ["composer", "development", "craftcms"],
"type": "composer-plugin",
"version": "1.0.2",
"version": "1.1.0",
"license": "MIT",
"autoload": {
"psr-4": {
Expand Down
48 changes: 43 additions & 5 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class Plugin implements PluginInterface, EventSubscriberInterface
{

const CRAFT_VERSION_WITH_PROJECT_CONFIG_SUPPORT = '3.1.0';

/**
* @var Composer
*/
Expand All @@ -41,7 +43,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface
public static function getSubscribedEvents()
{
return [
ScriptEvents::POST_INSTALL_CMD => 'runMigration'
ScriptEvents::POST_INSTALL_CMD => 'runCommands'
];
}

Expand All @@ -58,9 +60,12 @@ public function activate(Composer $composer, IOInterface $io)
}

/**
* Run the migration only if Craft is installed
* Script runner
*
* Runs migrate/all only if Craft is installed
* Runs project-config/sync if enabled in config/general.php
*/
public function runMigration()
public function runCommands()
{
$this->bootstrapCraft();

Expand All @@ -69,20 +74,32 @@ public function runMigration()
}

if (!$this->craft->getIsInstalled()) {
$this->io->writeError("Craft is not installed yet. No need to run migrations.");
$this->io->writeError("Craft is not installed yet. No need to run scripts.");
return true;
}

$this->io->write(PHP_EOL . "▶ <info>Craft auto migrate</info> [START]");

// migrate/all
try {
$this->craft->runAction('migrate/all', ['interactive' => 0]);
} catch (Exception $exception) {
$this->io->writeError("Craft auto migrate [ERROR]");
$this->io->writeError("Craft auto migrate [migrate/all ERROR]");
return false;
}

// project-config/sync
if ($this->useProjectConfigFile()) {
try {
$this->craft->runAction('project-config/sync', ['interactive' => 0]);
} catch (Exception $exception) {
$this->io->writeError("Craft auto migrate [project-config/sync ERROR]");
return false;
}
}

$this->io->write("▶ <info>Craft auto migrate</info> [END]" . PHP_EOL);

return true;

}
Expand Down Expand Up @@ -135,4 +152,25 @@ protected function bootstrapCraft()
return true;
}

/**
* @return bool
*/
protected function useProjectConfigFile(): bool
{
if (!$this->craft instanceof Application) {
return false;
}
if (version_compare($this->craft->getVersion(), self::CRAFT_VERSION_WITH_PROJECT_CONFIG_SUPPORT) < 0) {
return false;
}
if (!$this->craft->getConfig()->getGeneral()->hasProperty('useProjectConfigFile')) {
return false;
}
if ($this->craft->getConfig()->getGeneral()->useProjectConfigFile) {
return true;
}

return false;
}

}

0 comments on commit 432ea2e

Please sign in to comment.