Plugin.php 10.8 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
<?php
/**
 *
 *
 *
 *
 */

namespace MagentoHackathon\Composer\Magento;

use Composer\Autoload\AutoloadGenerator;
use Composer\Autoload\ClassMapGenerator;
use Composer\EventDispatcher\EventDispatcher;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PluginEvents;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Script\ScriptEvents;
use Composer\Installer\PackageEvents;
use Composer\Util\Filesystem;
use Symfony\Component\Process\Process;

class Plugin implements PluginInterface, EventSubscriberInterface
{

    /**
     * @var IOInterface
     */
    protected $io;


    /**
     * @var ProjectConfig
     */
    protected $config;


    /**
     * @var DeployManager
     */
    protected $deployManager;


    /**
     * @var Composer
     */
    protected $composer;

    /**
     * @var Installer
     */
    private $installer;

    /**
     * @var Filesystem
     */
    protected $filesystem;

    /**
     * @var string
     */
    private $regenerate = '/.regenerate';

    /**
     * @var string
     */
    private $varFolder = '/var';

    protected function initDeployManager(Composer $composer, IOInterface $io)
    {
        $this->deployManager = new DeployManager($io);

        $extra = $composer->getPackage()->getExtra();
        $sortPriority = isset($extra['magento-deploy-sort-priority']) ? $extra['magento-deploy-sort-priority'] : array();
        $this->deployManager->setSortPriority($sortPriority);

    }


    public function activate(Composer $composer, IOInterface $io)
    {
        $this->io = $io;
        $this->composer = $composer;
        $this->filesystem = new Filesystem();
        $this->config = new ProjectConfig($composer->getPackage()->getExtra());
        $this->installer = new Installer($io, $composer);
        $this->initDeployManager($composer, $io);
        $this->installer->setDeployManager($this->deployManager);
        $this->installer->setConfig($this->config);
        if ($this->io->isDebug()) {
            $this->io->write('activate magento plugin');
        }
        $composer->getInstallationManager()->addInstaller($this->installer);
    }

    public static function getSubscribedEvents()
    {
        return array(
            PluginEvents::COMMAND => array(
                array('onCommandEvent', 0),
            ),
            ScriptEvents::POST_INSTALL_CMD => array(
                array('onNewCodeEvent', 0),
            ),
            ScriptEvents::POST_UPDATE_CMD => array(
                array('onNewCodeEvent', 0),
            ),
            PackageEvents::POST_PACKAGE_UNINSTALL => array(
                array('onPackageUnistall', 0),
            )
        );
    }

    public function onPackageUnistall(\Composer\Installer\PackageEvent $event)
    {
        $ds = DIRECTORY_SEPARATOR;
        $package = $event->getOperation()->getPackage();
        list($vendor, $packageName) = explode('/', $package->getPrettyName());
        $packageName = trim(str_replace('module-', '', $packageName));
        $packageInstallationPath = $packageInstallationPath = $this->installer->getTargetDir();
        $packagePath = ucfirst($vendor) . $ds . str_replace(' ', '', ucwords(str_replace('-', ' ', $packageName)));
        $this->io->write("Removing $packagePath");
        $libPath = 'lib' . $ds . 'internal' . $ds . $packagePath;
        $magentoPackagePath = 'app' . $ds . 'code' . $ds . $packagePath;
        $deployStrategy = $this->installer->getDeployStrategy($package);
        $deployStrategy->rmdirRecursive($packageInstallationPath . $ds . $libPath);
        $deployStrategy->rmdirRecursive($packageInstallationPath . $ds . $magentoPackagePath);
        $this->requestRegeneration();
    }

    /**
     * actually is triggered before anything got executed
     *
     * @param \Composer\Plugin\CommandEvent $event
     */
    public function onCommandEvent(\Composer\Plugin\CommandEvent $event)
    {
        $command = $event->getCommandName();
    }

    /**
     * event listener is named this way, as it listens for events leading to changed code files
     *
     * @param \Composer\Script\Event $event
     */
    public function onNewCodeEvent(\Composer\Script\Event $event)
    {
        if ($this->io->isDebug()) {
            $this->io->write('start magento deploy via deployManager');
        }

        $this->deployManager->doDeploy();
        $this->deployLibraries();
        $this->saveVendorDirPath($event->getComposer());
        $this->requestRegeneration();
        $this->setFilePermissions();
    }

    /**
     * Set permissions for files using extra->chmod from composer.json
     *
     * @return void
     */
    private function setFilePermissions()
    {
        $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages();
        $message = 'Check "chmod" section in composer.json of %s package.';

        foreach ($packages as $package) {
            $extra = $package->getExtra();
            if (!isset($extra['chmod']) || !is_array($extra['chmod'])) {
                continue;
            }

            $error = false;
            foreach ($extra['chmod'] as $chmod) {
                if (!isset($chmod['mask']) || !isset($chmod['path']) || strpos($chmod['path'], '..') !== false) {
                    $error = true;
                    continue;
                }

                $file = $this->installer->getTargetDir() . '/' . $chmod['path'];
                if (file_exists($file)) {
                    chmod($file, octdec($chmod['mask']));
                } else {
                    $this->io->writeError([
                        'File doesn\'t exist: ' . $chmod['path'],
                        sprintf($message, $package->getName())
                    ]);
                }
            }

            if ($error) {
                $this->io->writeError([
                    'Incorrect mask or file path.',
                    sprintf($message, $package->getName())
                ]);
            }
        }
    }

    protected function deployLibraries()
    {
        $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages();
        $autoloadDirectories = array();

        $libraryPath = $this->config->getLibraryPath();
        if ($libraryPath === null) {
            if ($this->io->isDebug()) {
                $this->io->write('jump over deployLibraries as no Magento libraryPath is set');
            }
            return;
        }


        $vendorDir = rtrim($this->composer->getConfig()->get('vendor-dir'), '/');

        $filesystem = $this->filesystem;
        $filesystem->removeDirectory($libraryPath);
        $filesystem->ensureDirectoryExists($libraryPath);

        foreach ($packages as $package) {
            /** @var PackageInterface $package */
            $packageConfig = $this->config->getLibraryConfigByPackagename($package->getName());
            if ($packageConfig === null) {
                continue;
            }
            if (!isset($packageConfig['autoload'])) {
                $packageConfig['autoload'] = array('/');
            }
            foreach ($packageConfig['autoload'] as $path) {
                $autoloadDirectories[] = $libraryPath . '/' . $package->getName() . "/" . $path;
            }
            if ($this->io->isDebug()) {
                $this->io->write('Magento deployLibraries executed for ' . $package->getName());
            }
            $libraryTargetPath = $libraryPath . '/' . $package->getName();
            $filesystem->removeDirectory($libraryTargetPath);
            $filesystem->ensureDirectoryExists($libraryTargetPath);
            $this->copyRecursive($vendorDir . '/' . $package->getPrettyName(), $libraryTargetPath);

        }

        $autoloadGenerator = new AutoloadGenerator(new EventDispatcher($this->composer, $this->io));
        $classmap = ClassMapGenerator::createMap($libraryPath);
        $executable = $this->composer->getConfig()->get('bin-dir') . '/phpab';
        if (!file_exists($executable)) {
            $executable = $this->composer->getConfig()->get('vendor-dir') . '/theseer/autoload/composer/bin/phpab';
        }
        if (file_exists($executable)) {
            if ($this->io->isDebug()) {
                $this->io->write('Magento deployLibraries executes autoload generator');
            }
            $process = new Process($executable . " -o {$libraryPath}/autoload.php  " . implode(' ', $autoloadDirectories));
            $process->run();
        } else {
            if ($this->io->isDebug()) {
                $this->io->write('Magento deployLibraries autoload generator not availabel, you should require "theseer/autoload"');
                var_dump($executable, getcwd());

            }
        }


    }


    /**
     * Copy then delete is a non-atomic version of {@link rename}.
     *
     * Some systems can't rename and also don't have proc_open,
     * which requires this solution.
     *
     * copied from \Composer\Util\Filesystem::copyThenRemove and removed the remove part
     *
     * @param string $source
     * @param string $target
     */
    protected function copyRecursive($source, $target)
    {
        $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
        $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
        $this->filesystem->ensureDirectoryExists($target);

        foreach ($ri as $file) {
            $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
            if ($file->isDir()) {
                $this->filesystem->ensureDirectoryExists($targetPath);
            } else {
                copy($file->getPathname(), $targetPath);
            }
        }

    }

    /**
     * Generate file with path to Composer 'vendor' dir to be used by the application
     *
     * @param \Composer\Composer $composer
     * @throws \UnexpectedValueException
     */
    private function saveVendorDirPath(Composer $composer)
    {
        $magentoDir = $this->installer->getTargetDir();
        $vendorDirPath = $this->filesystem->findShortestPath(
            $magentoDir,
            realpath($composer->getConfig()->get('vendor-dir')),
            true
        );
        $vendorPathFile = $magentoDir . '/app/etc/vendor_path.php';
        $content = <<<AUTOLOAD
<?php
/**
 * Path to Composer vendor directory
 */
return '$vendorDirPath';

AUTOLOAD;
        file_put_contents($vendorPathFile, $content);
    }

    /**
     * Force regeneration of var/di, var/cache, var/generation on next object manager invocation
     *
     * @return void
     */
    private function requestRegeneration()
    {
        if (is_writable($this->installer->getTargetDir() . $this->varFolder)) {
            $filename = $this->installer->getTargetDir() . $this->varFolder . $this->regenerate;
            touch($filename);
        }
    }
}