Skip to content

Commit 17f9bbf

Browse files
nsennac9s
nsenna
authored andcommitted
Adding support to different directory structure on PEAR package
--------------------------------------------------------------- Added structure key support to PEAR package generator. It accepts a comma separated list as value or a simple value. This change make package generation more flexible and not dependent on a fixed structure.
1 parent a991997 commit 17f9bbf

File tree

7 files changed

+296
-25
lines changed

7 files changed

+296
-25
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.onion
22
vendor
33
tests/tmp
4+
.idea/

src/Onion/Package/Package.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Package implements PackageInterface
2727
public $license;
2828
public $licenseUri;
2929

30-
/**
31-
* main stability
30+
/**
31+
* main stability
3232
*/
3333
public $stability;
3434

@@ -57,8 +57,8 @@ class Package implements PackageInterface
5757
*/
5858
public $deps = array();
5959

60-
/**
61-
* ConfigContainer object
60+
/**
61+
* @var \Onion\ConfigContainer
6262
*/
6363
public $config;
6464

@@ -69,15 +69,20 @@ class Package implements PackageInterface
6969

7070
public function getDefaultStructureConfig()
7171
{
72-
// directory structure
73-
return array(
74-
'doc' => array('doc', 'docs','examples'),
75-
'test' => (array) 'tests',
76-
'php' => (array) 'src',
77-
// xxx: better config for roles
78-
// 'script' => (array) 'bin',
79-
'data' => (array) 'data',
80-
);
72+
$configuredStructure = $this->config->get('structure');
73+
74+
if (empty($configuredStructure)) {
75+
return array(
76+
'doc' => array('doc', 'docs','examples'),
77+
'test' => (array) 'tests',
78+
'php' => (array) 'src',
79+
// xxx: better config for roles
80+
// 'script' => (array) 'bin',
81+
'data' => (array) 'data',
82+
);
83+
}
84+
85+
return $configuredStructure;
8186
}
8287

8388

src/Onion/PackageConfigReader.php

+38-12
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
* $pkgxml->generate('package.xml');
3838
*
3939
*/
40-
class PackageConfigReader
41-
implements LoggableInterface
40+
class PackageConfigReader
41+
implements LoggableInterface
4242
{
4343
public $config;
4444

@@ -76,7 +76,7 @@ function read($file)
7676

7777
$ini = null;
7878
try {
79-
$ini = parse_ini_file( $file , true );
79+
$ini = $this->parseFile($file);
8080
}
8181
catch( Exception $e ) {
8282
throw new Exception( "Package.ini: $file syntax error: " . $e->getMessage() );
@@ -166,29 +166,29 @@ function read($file)
166166
$requires = $config->get('require');
167167
if( ! $requires ) {
168168

169-
// use default core dependency
169+
// use default core dependency
170170
$logger->info2("* required section is not defined. use php 5.3 and pearinstaller 1.4 by default.",1);
171171
$pkginfo->deps[] = array(
172172
'type' => 'core',
173173
'name' => 'php',
174174
'version' => array( 'min' => '5.3' ),
175175
);
176-
$pkginfo->deps[] = array(
176+
$pkginfo->deps[] = array(
177177
'type' => 'core',
178178
'name' => 'pearinstaller',
179179
'version' => array( 'min' => '1.4' ),
180180
);
181181
}
182182

183183
if( $requires ) {
184-
foreach( $requires as $key => $value )
184+
foreach( $requires as $key => $value )
185185
{
186186
$type = $this->detectDependencyType( $key , $value );
187187
switch($type) {
188188

189189
case 'core':
190190
$version = SpecUtils::parseVersion( $value );
191-
$pkginfo->deps[] = array(
191+
$pkginfo->deps[] = array(
192192
'type' => 'core',
193193
'name' => $key,
194194
'version' => $version, /* [ min => , max => ] */
@@ -242,7 +242,7 @@ function detectDependencyType($key,$value = null)
242242
elseif( preg_match('/^ext(?:ension)?\/\w+/',$key) ) {
243243
return 'extension';
244244

245-
}
245+
}
246246
// todo: check if there is a resource for this.
247247
else {
248248
// otherwisze it's a package
@@ -257,15 +257,15 @@ function detectDependencyType($key,$value = null)
257257
function parseDependency($key,$value)
258258
{
259259
// format: {channel host}/{package name} = {version expression}
260-
if( preg_match('/^([a-zA-Z0-9.-]+)\/(\w+)$/' , $key , $regs ) )
260+
if( preg_match('/^([a-zA-Z0-9.-]+)\/(\w+)$/' , $key , $regs ) )
261261
{
262262
if( $value != 'conflict' )
263263
{
264264
return array(
265265
'type' => 'pear',
266266
'name' => $regs[2],
267267
'version' => SpecUtils::parseVersion($value),
268-
'resource' => array(
268+
'resource' => array(
269269
'type' => 'channel',
270270
'channel' => $regs[1],
271271
)
@@ -276,7 +276,7 @@ function parseDependency($key,$value)
276276
'type' => 'pear',
277277
'name' => $regs[2],
278278
'conflict' => 1,
279-
'resource' => array(
279+
'resource' => array(
280280
'type' => 'channel',
281281
'channel' => $regs[1],
282282
)
@@ -291,7 +291,7 @@ function parseDependency($key,$value)
291291
'version' => SpecUtils::parseVersion($value),
292292
);
293293
}
294-
elseif( preg_match('/^(\w+)$/',$key,$regs) )
294+
elseif( preg_match('/^(\w+)$/',$key,$regs) )
295295
{
296296
// PEAR package with URI format
297297
if( preg_match('/^https?:\/\//',$value) ) {
@@ -317,6 +317,32 @@ public function getResources()
317317
{
318318
}
319319

320+
private function parseFile($file)
321+
{
322+
$ini = parse_ini_file($file, true);
323+
if (isset($ini['structure'])) {
324+
$ini['structure'] = $this->parseCommaSeparatedListValue($ini['structure']);
325+
}
326+
327+
return $ini;
328+
}
329+
330+
private function parseCommaSeparatedListValue(array $entry)
331+
{
332+
foreach ($entry as $key => $value) {
333+
if (false !== strpos($value, ',')) {
334+
$list = explode(',', $value);
335+
foreach ($list as $item) {
336+
$parsedStructure[$key][] = trim($item);
337+
}
338+
} else {
339+
$parsedStructure[$key][] = $value;
340+
}
341+
}
342+
343+
return $parsedStructure;
344+
}
345+
320346
}
321347

322348

tests/Onion/Package/PackageTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace tests\Onion\Package;
4+
5+
class PackageTest extends \PHPUnit_Framework_TestCase
6+
{
7+
public function testShouldUseIniFileConfiguredDirectoryStructure()
8+
{
9+
$logger = new \CLIFramework\Logger();
10+
$logger->quiet();
11+
$config = new \Onion\PackageConfigReader();
12+
$config->setLogger($logger);
13+
14+
$package = $config->read(__DIR__ . '/fixtures/stub.ini');
15+
16+
$fileStructure = $package->getDefaultStructureConfig();
17+
18+
$this->assertEquals(array('lib'), $fileStructure['php']);
19+
}
20+
21+
public function testShouldUseDefaultDirectoryStructure()
22+
{
23+
$logger = new \CLIFramework\Logger();
24+
$logger->quiet();
25+
$config = new \Onion\PackageConfigReader();
26+
$config->setLogger($logger);
27+
28+
$package = $config->read(__DIR__ . '/fixtures/stub_with_no_structure.ini');
29+
30+
$fileStructure = $package->getDefaultStructureConfig();
31+
32+
$this->assertEquals(array('src'), $fileStructure['php']);
33+
}
34+
}

tests/Onion/Package/fixtures/stub.ini

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[package]
2+
name = Onion
3+
summary = A Simple PHP Packager Builder.
4+
desc = "Onion, The fast approch to make packages for PHP. Onion is
5+
able to generate a PEAR-compatible package.xml file from a very simple config file."
6+
version = 0.0.8
7+
stability = alpha
8+
9+
; homepage = http://php-onion.org ; optional
10+
; license = PHP ; optional, default to PHP
11+
; version.api = 0.0.1 ; optional, defualt to "version"
12+
; author = Yo-An Lin <[email protected]> # also works
13+
author = Yo-An Lin <[email protected]>
14+
authors[] = Yo-An Lin <[email protected]>
15+
channel = pear.corneltek.com
16+
17+
[require]
18+
php = 5.3
19+
pearinstaller = 1.4.1
20+
21+
; packages
22+
pear.corneltek.com/GetOptionKit = 0.0.2
23+
pear.corneltek.com/CLIFramework = 0.0.2
24+
pear.corneltek.com/Universal = 0.0.2
25+
pear.symfony-project.com/YAML = 0.0.0
26+
; pear.phpunit.de/PHPUnit = 0.0.0
27+
28+
; extensions
29+
; extension/reflection = 0
30+
; extension/ctype = 0
31+
extension/pcre = 0
32+
33+
34+
[require CLIFramework]
35+
git = http://github.com/c9s/CLIFramework
36+
type = pear
37+
38+
[require Asset]
39+
git = https://github.com/kriswallsmith/assetic.git
40+
; autoload = (
41+
; Assetic: src
42+
; )
43+
44+
[require Smarty]
45+
url = http://www.smarty.net/files/Smarty-3.1.8.tar.gz
46+
bootstrap = path/to/init.php
47+
48+
[resource phpan]
49+
type = pub
50+
host = pubphp.org
51+
52+
[resource openpear]
53+
type = pear
54+
host = openpear.org
55+
56+
[resource corneltek]
57+
type = pear
58+
host = pear.corneltek.com
59+
60+
[roles]
61+
onion.phar = script
62+
changes.* = doc
63+
*.md = doc
64+
65+
[repository]
66+
git = git://github.com/c9s/Onion.git
67+
68+
[structure]
69+
doc = doc, docs, examples
70+
test = tests
71+
php = lib
72+
data = data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[package]
2+
name = Onion
3+
summary = A Simple PHP Packager Builder.
4+
desc = "Onion, The fast approch to make packages for PHP. Onion is
5+
able to generate a PEAR-compatible package.xml file from a very simple config file."
6+
version = 0.0.8
7+
stability = alpha
8+
9+
; homepage = http://php-onion.org ; optional
10+
; license = PHP ; optional, default to PHP
11+
; version.api = 0.0.1 ; optional, defualt to "version"
12+
; author = Yo-An Lin <[email protected]> # also works
13+
author = Yo-An Lin <[email protected]>
14+
authors[] = Yo-An Lin <[email protected]>
15+
channel = pear.corneltek.com
16+
17+
[require]
18+
php = 5.3
19+
pearinstaller = 1.4.1
20+
21+
; packages
22+
pear.corneltek.com/GetOptionKit = 0.0.2
23+
pear.corneltek.com/CLIFramework = 0.0.2
24+
pear.corneltek.com/Universal = 0.0.2
25+
pear.symfony-project.com/YAML = 0.0.0
26+
; pear.phpunit.de/PHPUnit = 0.0.0
27+
28+
; extensions
29+
; extension/reflection = 0
30+
; extension/ctype = 0
31+
extension/pcre = 0
32+
33+
34+
[require CLIFramework]
35+
git = http://github.com/c9s/CLIFramework
36+
type = pear
37+
38+
[require Asset]
39+
git = https://github.com/kriswallsmith/assetic.git
40+
; autoload = (
41+
; Assetic: src
42+
; )
43+
44+
[require Smarty]
45+
url = http://www.smarty.net/files/Smarty-3.1.8.tar.gz
46+
bootstrap = path/to/init.php
47+
48+
[resource phpan]
49+
type = pub
50+
host = pubphp.org
51+
52+
[resource openpear]
53+
type = pear
54+
host = openpear.org
55+
56+
[resource corneltek]
57+
type = pear
58+
host = pear.corneltek.com
59+
60+
[roles]
61+
onion.phar = script
62+
changes.* = doc
63+
*.md = doc
64+
65+
[repository]
66+
git = git://github.com/c9s/Onion.git

0 commit comments

Comments
 (0)