|
| 1 | +<?php |
| 2 | + |
| 3 | +/*! |
| 4 | + * Installer Class |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Dave Olsen, http://dmolsen.com |
| 7 | + * Licensed under the MIT license |
| 8 | + * |
| 9 | + * References the InstallerUtil class that is included in pattern-lab/core |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PatternLab; |
| 14 | + |
| 15 | +use \Composer\Script\Event; |
| 16 | +use \Composer\Installer\PackageEvent; |
| 17 | +use \PatternLab\InstallerUtil; |
| 18 | + |
| 19 | +class Installer { |
| 20 | + |
| 21 | + protected static $installerInfo = array("projectInstall" => false, "packagesRemove" => false, "suggestedStarterKits" => array(), "configOverrides" => array(), "patternLabPackages" => array()); |
| 22 | + |
| 23 | + /** |
| 24 | + * Get any config overrides that may exist for the edition |
| 25 | + * @param {Object} a script event object from composer |
| 26 | + */ |
| 27 | + public static function getConfigOverrides(Event $event) { |
| 28 | + |
| 29 | + $extra = $event->getComposer()->getPackage()->getExtra(); |
| 30 | + if (isset($extra["patternlab"]) && isset($extra["patternlab"]["config"]) && is_array($extra["patternlab"]["config"])) { |
| 31 | + self::$installerInfo["configOverrides"] = $extra["patternlab"]["config"]; |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the package info from each patternlab-* package's composer.json |
| 38 | + * @param {String} the type of event fired during the composer install |
| 39 | + * @param {Object} a script event object from composer |
| 40 | + */ |
| 41 | + public static function getPackageInfo($type, $event) { |
| 42 | + |
| 43 | + $package = ($type == "update") ? $event->getOperation()->getTargetPackage() : $event->getOperation()->getPackage(); |
| 44 | + $packageType = $package->getType(); |
| 45 | + $packageExtra = $package->getExtra(); |
| 46 | + $packageInfo = array(); |
| 47 | + |
| 48 | + // make sure we're only evaluating pattern lab packages |
| 49 | + if (strpos($packageType,"patternlab-") !== false) { |
| 50 | + |
| 51 | + $packageInfo["name"] = $package->getName(); |
| 52 | + $packageInfo["type"] = $packageType; |
| 53 | + $packageInfo["pathBase"] = $event->getComposer()->getInstallationManager()->getInstallPath($package); |
| 54 | + $packageInfo["pathDist"] = $packageInfo["pathBase"].DIRECTORY_SEPARATOR."dist".DIRECTORY_SEPARATOR; |
| 55 | + $packageInfo["extra"] = (isset($packageExtra["patternlab"])) ? $packageExtra["patternlab"] : array(); |
| 56 | + |
| 57 | + self::$installerInfo["packages"][] = $packageInfo; |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the suggested starter kits from the root package composer.json |
| 65 | + * @param {Object} a script event object from composer |
| 66 | + */ |
| 67 | + public static function getSuggestedStarterKits(Event $event) { |
| 68 | + |
| 69 | + $extra = $event->getComposer()->getPackage()->getExtra(); |
| 70 | + if (isset($extra["patternlab"]) && isset($extra["patternlab"]["starterKitSuggestions"]) && is_array($extra["patternlab"]["starterKitSuggestions"])) { |
| 71 | + self::$installerInfo["suggestedStarterKits"] = $extra["patternlab"]["starterKitSuggestions"]; |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Run the centralized postInstallCmd |
| 78 | + * @param {Object} a script event object from composer |
| 79 | + */ |
| 80 | + public static function postInstallCmd(Event $event) { |
| 81 | + |
| 82 | + InstallerUtil::postInstallCmd(self::$installerInfo, $event); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Run the centralized postUpdateCmd |
| 88 | + * @param {Object} a script event object from composer |
| 89 | + */ |
| 90 | + public static function postUpdateCmd(Event $event) { |
| 91 | + |
| 92 | + InstallerUtil::postUpdateCmd(self::$installerInfo, $event); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Clean-up when a package is removed |
| 98 | + * @param {Object} a script event object from composer |
| 99 | + */ |
| 100 | + public static function postPackageInstall(PackageEvent $event) { |
| 101 | + |
| 102 | + self::getPackageInfo("install", $event); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Clean-up when a package is removed |
| 108 | + * @param {Object} a script event object from composer |
| 109 | + */ |
| 110 | + public static function postPackageUpdate(PackageEvent $event) { |
| 111 | + |
| 112 | + self::getPackageInfo("update", $event); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Clean-up when a package is removed |
| 118 | + * @param {Object} a script event object from composer |
| 119 | + */ |
| 120 | + public static function prePackageUninstall(PackageEvent $event) { |
| 121 | + |
| 122 | + // make sure the postUpdateCmd doesnt actually do anything |
| 123 | + self::setPackagesRemove(); |
| 124 | + |
| 125 | + // get the basic package info |
| 126 | + $package = $event->getOperation()->getPackage(); |
| 127 | + $packageType = $package->getType(); |
| 128 | + $packageInfo = array(); |
| 129 | + |
| 130 | + // make sure we're only evaluating pattern lab packages. remove attributes related to them. |
| 131 | + if (strpos($packageType,"patternlab-") !== false) { |
| 132 | + |
| 133 | + $packageInfo["name"] = $package->getName(); |
| 134 | + $packageInfo["type"] = $packageType; |
| 135 | + $packageInfo["pathBase"] = $event->getComposer()->getInstallationManager()->getInstallPath($package); |
| 136 | + |
| 137 | + InstallerUtil::packageRemove($packageInfo); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Set the packages remove boolean to true |
| 145 | + */ |
| 146 | + public static function setPackagesRemove() { |
| 147 | + |
| 148 | + self::$installerInfo["packagesRemove"] = true; |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Set the project install boolean to true |
| 154 | + * @param {Object} a script event object from composer |
| 155 | + */ |
| 156 | + public static function setProjectInstall(Event $event) { |
| 157 | + |
| 158 | + self::$installerInfo["projectInstall"] = true; |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments