Completed
Branch master (3fe817)
by Marko
02:16
created

AframeInstallerPlugin::activate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 2
b 1
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 26, 2016 - 3:59:35 PM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         AframeInstaller.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 ^ @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Composer\Installer;
25
26
use \Composer\Composer;
27
use \Composer\IO\IOInterface;
28
use \Composer\Plugin\PluginInterface;
29
30
class AframeInstallerPlugin implements PluginInterface
31
{
32
33
    /**
34
     * Apply plugin modifications to Composer
35
     *
36
     * @param Composer $composer            
37
     * @param IOInterface $io            
38
     */
39
    public function activate(Composer $composer, IOInterface $io)
40
    {
41
        $aframe_installer = new AframeComponentInstaller($io, $composer);
42
        $composer->getInstallationManager()->addInstaller($aframe_installer);
43
        $info_msg = sprintf('A-Frame installer plugin is added as custom installer! composer-plugin-api v: %s', self::PLUGIN_API_VERSION);
44
        $io->write($info_msg);
45
        
46
        /* Update A-Frame core */
47
        if ($this->requiresAframeCoreUpdate($aframe_installer))
48
            $this->updateAframeCore($aframe_installer);
49
        
50
        $aframe_installer->updateConfig();
51
    }
52
53
    /**
54
     * Update A-Frame core
55
     *
56
     * Aframe core is always installed as dependency
57
     * so therefore it is installed/updated before
58
     * this pulgin is loaded or updated.
59
     *
60
     * @param AframeComponentInstaller $aframe_installer            
61
     * @return void
62
     */
63
    private function updateAframeCore(AframeComponentInstaller $aframe_installer)
64
    {
65
        $source = $aframe_installer->getAframeCoreSrcDir();
66
        $dest = $aframe_installer->getPublicAframeCoreDir();
67
        
68
        $aframe_installer->copy($source, $dest);
69
    }
70
71
    /**
72
     * Is update of Aframe is required
73
     *
74
     * @param AframeComponentInstaller $aframe_installer            
75
     * @return bool
76
     */
77
    protected function requiresAframeCoreUpdate(AframeComponentInstaller $aframe_installer)
78
    {
79
        $aframe_core_dir = $aframe_installer->getPublicAframeCoreDir();
80
        $aframe_core_file = $aframe_core_dir . DIRECTORY_SEPARATOR . 'aframe.js';
81
        $aframe_src_dir = $aframe_installer->getAframeCoreSrcDir();
82
        
83
        return (! is_dir($aframe_core_dir) || ! file_exists($aframe_core_file) || ! is_dir($aframe_src_dir)) ? true : filemtime($aframe_src_dir) > filemtime($aframe_core_dir);
84
    }
85
}
86