Skip to content

Commit a6cec53

Browse files
committed
Add bin/install.php and remove shell script installers
1 parent 9a29f5d commit a6cec53

5 files changed

Lines changed: 203 additions & 120 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,25 @@ codeigniter/
3535
$ composer create-project kenjis/codeigniter-composer-installer codeigniter
3636
```
3737

38-
If you want to install translations for system messages (requires shell):
38+
If you want to install translations for system messages:
3939

4040
```
4141
$ cd /path/to/codeigniter
42-
$ bin/install-translations.sh
42+
$ php bin/install.php translations 3.0.0
4343
```
4444

45-
#### Install Third Party Libraries (Requires Shell)
45+
#### Install Third Party Libraries
4646

4747
[Codeigniter Matches CLI](https://github.com/avenirer/codeigniter-matches-cli):
4848

4949
```
50-
$ bin/install-codeigniter-matches-cli.sh
50+
$ php bin/install.php matches-cli master
5151
```
5252

5353
[CodeIgniter HMVC Modules](https://github.com/jenssegers/codeigniter-hmvc-modules):
5454

5555
```
56-
$ bin/install-codeigniter-hmvc-modules.sh
56+
$ php bin/install.php hmvc-modules master
5757
```
5858

5959
### Run PHP built-in server (PHP 5.4 or later)

bin/install-codeigniter-hmvc-modules.sh

Lines changed: 0 additions & 38 deletions
This file was deleted.

bin/install-codeigniter-matches-cli.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

bin/install-translations.sh

Lines changed: 0 additions & 37 deletions
This file was deleted.

bin/install.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$installer = new Installer();
5+
6+
if ($argc === 3) {
7+
$package = $argv[1];
8+
$version = $argv[2];
9+
echo $installer->install($package, $version);
10+
} else {
11+
echo $installer->usage($argv[0]);
12+
}
13+
14+
15+
class Installer
16+
{
17+
protected $tmp_dir;
18+
protected $packages = array();
19+
20+
public function __construct() {
21+
$this->tmp_dir = __DIR__ . '/tmp';
22+
@mkdir($this->tmp_dir);
23+
24+
$this->packages = [
25+
'translations' => array(
26+
'repos' => 'github',
27+
'user' => 'bcit-ci',
28+
'repos' => 'codeigniter3-translations',
29+
'name' => 'Translations for CodeIgniter System Messages',
30+
'dir' => 'language',
31+
),
32+
'matches-cli' => array(
33+
'repos' => 'github',
34+
'user' => 'avenirer',
35+
'repos' => 'codeigniter-matches-cli',
36+
'name' => 'Codeigniter Matches CLI',
37+
'dir' => array('config', 'controllers', 'views'),
38+
'msg' => 'See http://avenirer.github.io/codeigniter-matches-cli/',
39+
),
40+
'hmvc-modules' => array(
41+
'repos' => 'github',
42+
'user' => 'jenssegers',
43+
'repos' => 'codeigniter-hmvc-modules',
44+
'name' => 'CodeIgniter HMVC Modules (jenssegers)',
45+
'dir' => array('core', 'third_party'),
46+
'msg' => 'See https://github.com/jenssegers/codeigniter-hmvc-modules#installation',
47+
),
48+
];
49+
}
50+
51+
public function usage($self)
52+
{
53+
$msg = 'You can install:' . PHP_EOL;
54+
55+
foreach ($this->packages as $key => $value) {
56+
$msg .= ' ' . $value['name'] . ' (' . $key . ')' . PHP_EOL;
57+
}
58+
59+
$msg .= PHP_EOL;
60+
$msg .= 'Usage:' . PHP_EOL;
61+
$msg .= ' php install.php <package> <version/branch>' . PHP_EOL;
62+
$msg .= PHP_EOL;
63+
$msg .= 'Examples:' . PHP_EOL;
64+
$msg .= " php $self translations 3.0.0" . PHP_EOL;
65+
$msg .= " php $self translations develop" . PHP_EOL;
66+
$msg .= " php $self matches-cli master" . PHP_EOL;
67+
$msg .= " php $self hmvc-modules master" . PHP_EOL;
68+
69+
return $msg;
70+
}
71+
72+
public function install($package, $version)
73+
{
74+
if (! isset($this->packages[$package]))
75+
{
76+
return 'Error! no such package: ' . $package . PHP_EOL;
77+
}
78+
79+
// github
80+
list($src, $dst) = $this->downloadFromGithub($package, $version);
81+
82+
$this->recursiveCopy($src, $dst);
83+
$this->recursiveUnlink($this->tmp_dir);
84+
85+
$msg = 'Installed: ' . $package .PHP_EOL;
86+
if (isset($this->packages[$package]['msg'])) {
87+
$msg .= $this->packages[$package]['msg'] . PHP_EOL;
88+
}
89+
return $msg;
90+
}
91+
92+
private function downloadFromGithub($package, $version)
93+
{
94+
$user = $this->packages[$package]['user'];
95+
$repos = $this->packages[$package]['repos'];
96+
$url = "https://github.com/$user/$repos/archive/$version.zip";
97+
$filepath = $this->download($url);
98+
$this->unzip($filepath);
99+
100+
$dir = $this->packages[$package]['dir'];
101+
102+
if (is_string($dir)) {
103+
$src = realpath(dirname($filepath) . "/$repos-$version/$dir");
104+
$dst = realpath(__DIR__ . "/../application/$dir");
105+
return [$src, $dst];
106+
}
107+
108+
foreach ($dir as $directory) {
109+
$src[] = realpath(dirname($filepath) . "/$repos-$version/$directory");
110+
$dst[] = realpath(__DIR__ . "/../application/$directory");
111+
}
112+
return [$src, $dst];
113+
}
114+
115+
private function download($url)
116+
{
117+
$file = file_get_contents($url);
118+
if ($file === false) {
119+
throw new RuntimeException("Can't download: $url");
120+
}
121+
echo 'Downloaed: ' . $url . PHP_EOL;
122+
123+
$urls = parse_url($url);
124+
$filepath = $this->tmp_dir . '/' . basename($urls['path']);
125+
file_put_contents($filepath, $file);
126+
127+
return $filepath;
128+
}
129+
130+
private function unzip($filepath)
131+
{
132+
$zip = new ZipArchive();
133+
if ($zip->open($filepath) === TRUE) {
134+
$zip->extractTo($this->tmp_dir . '/');
135+
$zip->close();
136+
} else {
137+
throw new RuntimeException('Failed to unzip: ' . $filepath);
138+
}
139+
}
140+
141+
/**
142+
* Recursive Copy
143+
*
144+
* @param string $src
145+
* @param string $dst
146+
*/
147+
private function recursiveCopy($src, $dst)
148+
{
149+
if (is_array($src)) {
150+
foreach ($src as $key => $source) {
151+
$this->recursiveCopy($source, $dst[$key]);
152+
}
153+
154+
return;
155+
}
156+
157+
@mkdir($dst, 0755);
158+
159+
$iterator = new \RecursiveIteratorIterator(
160+
new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS),
161+
\RecursiveIteratorIterator::SELF_FIRST
162+
);
163+
164+
foreach ($iterator as $file) {
165+
if ($file->isDir()) {
166+
@mkdir($dst . '/' . $iterator->getSubPathName());
167+
} else {
168+
$success = copy($file, $dst . '/' . $iterator->getSubPathName());
169+
if ($success) {
170+
echo 'copied: ' . $dst . '/' . $iterator->getSubPathName() . PHP_EOL;
171+
}
172+
}
173+
}
174+
}
175+
176+
/**
177+
* Recursive Unlink
178+
*
179+
* @param string $dir
180+
*/
181+
private function recursiveUnlink($dir)
182+
{
183+
$iterator = new \RecursiveIteratorIterator(
184+
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
185+
\RecursiveIteratorIterator::CHILD_FIRST
186+
);
187+
188+
foreach ($iterator as $file) {
189+
if ($file->isDir()) {
190+
rmdir($file);
191+
} else {
192+
unlink($file);
193+
}
194+
}
195+
196+
rmdir($dir);
197+
}
198+
}

0 commit comments

Comments
 (0)