Skip to content

Commit dd39afb

Browse files
committed
Redesign new config reader
1 parent 465dd39 commit dd39afb

9 files changed

+245
-11
lines changed

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
backupGlobals="false"
66
verbose="true"
77
syntaxCheck="true"
8+
colors="true"
89
convertErrorsToExceptions="true"
910
convertNoticesToExceptions="true"
1011
convertWarningsToExceptions="true"

src/Onion/Command/BuildCommand.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ function execute($arguments = array())
6868

6969

7070
$logger->info( 'Configuring package.ini' );
71-
$config = new PackageConfigReader($logger);
72-
$config->readAsPackageXml();
71+
$config = new PackageConfigReader();
72+
$config->setLogger( $logger );
73+
$config->read( 'package.ini' );
74+
75+
# $config->readAsPackageXml();
7376
$xml = $config->generatePackageXml();
7477

7578
/*

src/Onion/ConfigContainer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ function get( $refstr )
8787
if( isset($ref[$path]) ) {
8888
$ref = & $ref[$path];
8989
} else {
90+
return null;
9091
// debug_print_backtrace();
91-
throw new Exception("config key $refstr is undefined.");
92+
// throw new Exception("config key $refstr is undefined.");
9293
}
9394
}
9495
return $ref;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/*
3+
* This file is part of the Onion package.
4+
*
5+
* (c) Yo-An Lin <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
*/
11+
namespace Onion\Exception;
12+
use Exception;
13+
14+
class InvalidConfigException extends Exception
15+
{
16+
17+
}
18+

src/Onion/LoggableInterface.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
interface LoggableInterface
1414
{
1515
function setLogger( \CLIFramework\Logger $logger );
16-
function getLogger();
16+
17+
// we use __call magic to call logger
18+
// function info($msg,$level = 0);
19+
// function info2($msg,$level = 0);
1720
}
1821

src/Onion/Package.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/*
3+
* This file is part of the Onion package.
4+
*
5+
* (c) Yo-An Lin <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
*/
11+
namespace Onion;
12+
13+
class Package
14+
{
15+
16+
public $name;
17+
public $version;
18+
public $desc;
19+
20+
/**
21+
* ConfigContainer object
22+
*/
23+
public $config;
24+
25+
26+
}
27+
28+
29+

src/Onion/PackageConfigReader.php

+96-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,97 @@
3636
* $pkgxml->generate('package.xml');
3737
*
3838
*/
39-
class PackageConfigReader
39+
class PackageConfigReader
40+
implements LoggableInterface
41+
{
42+
public $logger;
43+
44+
function __construct()
45+
{
46+
}
47+
48+
function setLogger( \CLIFramework\Logger $logger)
49+
{
50+
$this->logger = $logger;
51+
}
52+
53+
function getLogger()
54+
{
55+
return $this->logger;
56+
}
57+
58+
function __call($name,$arguments)
59+
{
60+
if( $this->logger )
61+
call_user_func_array( array($this->logger,$name) , $arguments );
62+
}
63+
64+
function read($file)
65+
{
66+
$logger = $this->getLogger();
67+
$ini = null;
68+
try {
69+
$ini = parse_ini_file( $file , true );
70+
}
71+
catch( Exception $e ) {
72+
throw new Exception( "Package.ini: $file syntax error: " . $e->getMessage() );
73+
}
74+
75+
if( ! $ini )
76+
throw new Exception( "$file is empty." );
77+
78+
$config = new ConfigContainer( $ini );
79+
80+
// preprocess, validate sections only for package.ini
81+
$pkginfo = new Package;
82+
$pkginfo->config = $config;
83+
84+
// validation
85+
$requiredFields = explode(' ','package.name package.desc package.version');
86+
foreach( $requiredFields as $f ) {
87+
if( ! $config->has( $f ) )
88+
throw new \Onion\Exception\InvalidConfigException( "$f is not defined." );
89+
}
90+
91+
if( ! $config->has('package.authors') && ! $config->has('package.author') ) {
92+
echo <<<EOT
93+
Attribute 'author' or 'authors' is not defined.
94+
Please define 'author' in your package.ini file:
95+
96+
[package]
97+
author = Name <[email protected]>
98+
EOT;
99+
throw new \Onion\Exception\InvalidConfigException('package.author or package.authors is not defined.');
100+
}
101+
102+
103+
// set default values
104+
if( ! $config->has('package.summary') ) {
105+
$logger->debug("* summary is not defined., use the first paragraph from description by default.",1);
106+
$descs = explode("\n",$config->get('package.desc'));
107+
$config->set('package.summary',$descs[0]); # use first line desc as summary by default.
108+
}
109+
110+
if( ! $config->has('package.license') ) {
111+
$logger->debug("* license is not defined., use PHP license by default.",1);
112+
$config->set('package.license','PHP');
113+
}
114+
115+
// build package meta info
116+
$pkginfo->name = $config->get('package.name');
117+
$pkginfo->desc = $config->get('package.desc');
118+
$pkginfo->summary = $config->get('package.summary');
119+
$pkginfo->version = $config->get('package.version');
120+
$pkginfo->stability = $config->get('package.stability');
121+
122+
123+
124+
return $pkginfo;
125+
}
126+
127+
}
128+
129+
class PackageConfigReader2
40130
{
41131
public $file;
42132
public $config;
@@ -101,12 +191,6 @@ function readAsPackageXml()
101191

102192

103193
/* check optional attributes */
104-
105-
if( ! $config->has('package.summary') ) {
106-
$descs = explode("\n",$config->get('package.desc'));
107-
$config->set('package.summary',$descs[0]); # use first line desc as summary by default.
108-
}
109-
110194
if( ! $config->has('package.license') ) {
111195
$logger->info2("* license is not defined., use PHP license by default.",1);
112196
$config->set('package.license','PHP LICENSE');
@@ -121,6 +205,11 @@ function readAsPackageXml()
121205
* <license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
122206
*/
123207

208+
209+
210+
/**
211+
* package xml must have some default value
212+
*/
124213
if( ! $config->has('package.channel' ) ) {
125214
$logger->info2("* package channel is not defined. use pear.php.net by default.",1);
126215
$config->set('package.channel','pear.php.net');
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
* This file is part of the Onion package.
4+
*
5+
* (c) Yo-An Lin <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
*/
11+
namespace Onion;
12+
use PHPUnit_Framework_TestCase;
13+
class PackageConfigReaderTest extends PHPUnit_Framework_TestCase
14+
{
15+
function test()
16+
{
17+
$logger = new \CLIFramework\Logger;
18+
ok( $logger );
19+
20+
$reader = new PackageConfigReader;
21+
$reader->setLogger( $logger );
22+
23+
ob_start();
24+
$reader->info( 'msg' );
25+
$reader->error( 'msg' );
26+
$reader->info2( 'msg' );
27+
ob_end_clean();
28+
29+
ok( $reader );
30+
31+
ob_start();
32+
$pkg = $reader->read( 'tests/data/package.ini' );
33+
ob_end_clean();
34+
35+
ok( $pkg );
36+
37+
38+
}
39+
}
40+

tests/data/package.ini

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
26+
Test = resource
27+
28+
29+
; extensions
30+
; extension/reflection = 0
31+
; extension/ctype = 0
32+
extension/pcre = 0
33+
34+
[resource UrlLibrary]
35+
type = library
36+
url = http://foo.com/path/to/lib.tgz
37+
library = src
38+
autoload = PSR-0
39+
40+
[resource CLIFramework]
41+
type = pear
42+
git = http://github.com/c9s/CLIFramework.git
43+
44+
[roles]
45+
onion.phar = script
46+
changes.* = doc
47+
*.md = doc
48+
49+
[repository]
50+
git = git://github.com/c9s/Onion.git

0 commit comments

Comments
 (0)