Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12694 anikendra 1
<?php
2
 
3
class Spark_type {
4
 
5
    function __construct($data)
6
    {
7
        $this->data = $data;
8
        $this->name = $this->data->name;
9
        $this->spark_id = property_exists($this->data, 'id') ? $this->data->id : null;
10
        $this->version = property_exists($this->data, 'version') ? $this->data->version : null;
11
        $this->tag = property_exists($this->data, 'tag') ? $this->data->tag : $this->version;
12
        $this->base_location = property_exists($this->data, 'base_location') ? $this->data->base_location : null;
13
 
14
        // Load the dependencies
15
        $this->dependencies = property_exists($this->data, 'dependencies') ? $this->data->dependencies : array();
16
 
17
        // Assign other data we don't have
18
        foreach ($this->data as $k=>$v)
19
        {
20
            if (!property_exists($this, $k)) $this->$k = $v;
21
        }
22
 
23
        // used internally
24
        $this->temp_token = 'spark-' . $this->spark_id . '-' . time();
25
        $this->temp_path = sys_get_temp_dir() . '/' . $this->temp_token;
26
    }
27
 
28
    final function installed_path()
29
    {
30
        return $this->installed_path;
31
    }
32
 
33
    function location_detail() { }
34
    function retrieve() { }
35
 
36
    function install()
37
    {
38
        foreach ($this->dependencies as $dependency)
39
        {
40
            if ($dependency->is_direct)
41
            {
42
                $this->install_dependency($dependency);
43
            }
44
        }
45
 
46
        @mkdir(SPARK_PATH); // Two steps for windows
47
        @mkdir(SPARK_PATH . "/$this->name");
48
        Spark_utils::full_move($this->temp_path, $this->installation_path);
49
        Spark_utils::remove_full_directory($this->temp_path);
50
        $this->installed_path = $this->installation_path;
51
    }
52
 
53
    private function recurseMove($src,$dst)
54
    {
55
        $dir = opendir($src);
56
        @mkdir($dst);
57
        while(false !== ( $file = readdir($dir)) ) {
58
            if (( $file != '.' ) && ( $file != '..' )) {
59
                if ( is_dir($src . '/' . $file) ) {
60
                    $this->recurseMove($src . '/' . $file,$dst . '/' . $file);
61
                }
62
                else {
63
                    rename($src . '/' . $file,$dst . '/' . $file);
64
                }
65
            }
66
        }
67
        closedir($dir);
68
    }
69
 
70
    private function rrmdir($dir)
71
    {
72
        if (is_dir($dir)) {
73
            $files = scandir($dir);
74
            foreach ($files as $file) {
75
                if ($file != "." && $file != "..") {
76
                    if (is_dir($dir . "/" . $file)) {
77
                        $this->rrmdir($dir . "/" . $file);
78
                    }
79
                    else {
80
                        unlink($dir . "/" . $file);
81
                    }
82
                }
83
            }
84
            reset($files);
85
            rmdir($dir);
86
        }
87
    }
88
 
89
    function install_dependency($dependency_data) {
90
        // Get the spark object
91
        $spark = null;
92
        if ($dependency_data->repository_type == 'hg') $spark = Mercurial_spark::get_spark($dependency_data);
93
        else if ($dependency_data->repository_type == 'git') $spark = Git_spark::get_spark($dependency_data);
94
        else if ($dependency_data->repository_type == 'zip') $spark = new Zip_spark($dependency_data);
95
        else throw new Exception('Unknown repository type: ' . $dependency_data->repository_type);
96
        // Install the spark
97
        if ($spark->verify(false)) { // if not installed, install
98
            $spark->retrieve();
99
            $spark->install();
100
            Spark_utils::notice("Installed dependency: $spark->name to " . $spark->installed_path());
101
        }
102
        else {
103
            Spark_utils::warning("Dependency $spark->name is already installed.");
104
        }
105
    }
106
 
107
    function verify($break_on_already_installed = true)
108
    {
109
        // see if this is deactivated
110
        if ($this->data->is_deactivated)
111
        {
112
            $msg = 'Woah there - it seems the spark you want has been deactivated';
113
            if ($this->data->spark_home) $msg .= "\nLook for different versions at: " . $this->data->spark_home;
114
            throw new Spark_exception($msg);
115
        }
116
        // see if this is unsupported
117
        if ($this->data->is_unsupported)
118
        {
119
            Spark_utils::warning('This spark is no longer supported.');
120
            Spark_utils::warning('You can keep using it, or look for an alternate');
121
        }
122
        // tell the user if its already installed and throw an error
123
        $this->installation_path = SPARK_PATH . "/$this->name/$this->version";
124
        if (is_dir($this->installation_path))
125
        {
126
            if ($break_on_already_installed)
127
            {
128
                throw new Spark_exception("Already installed.  Try `php tools/spark reinstall $this->name`");
129
            }
130
            return false;
131
        }
132
        else
133
        {
134
            return true;
135
        }
136
    }
137
 
138
}