Skip to content

Commit

Permalink
Redesign new config reader
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 22, 2011
1 parent 465dd39 commit dd39afb
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 11 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Expand Up @@ -5,6 +5,7 @@
backupGlobals="false"
verbose="true"
syntaxCheck="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
Expand Down
7 changes: 5 additions & 2 deletions src/Onion/Command/BuildCommand.php
Expand Up @@ -68,8 +68,11 @@ function execute($arguments = array())


$logger->info( 'Configuring package.ini' );
$config = new PackageConfigReader($logger);
$config->readAsPackageXml();
$config = new PackageConfigReader();
$config->setLogger( $logger );
$config->read( 'package.ini' );

# $config->readAsPackageXml();
$xml = $config->generatePackageXml();

/*
Expand Down
3 changes: 2 additions & 1 deletion src/Onion/ConfigContainer.php
Expand Up @@ -87,8 +87,9 @@ function get( $refstr )
if( isset($ref[$path]) ) {
$ref = & $ref[$path];
} else {
return null;
// debug_print_backtrace();
throw new Exception("config key $refstr is undefined.");
// throw new Exception("config key $refstr is undefined.");
}
}
return $ref;
Expand Down
18 changes: 18 additions & 0 deletions src/Onion/Exception/InvalidConfigException.php
@@ -0,0 +1,18 @@
<?php
/*
* This file is part of the Onion package.
*
* (c) Yo-An Lin <cornelius.howl@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace Onion\Exception;
use Exception;

class InvalidConfigException extends Exception
{

}

5 changes: 4 additions & 1 deletion src/Onion/LoggableInterface.php
Expand Up @@ -13,6 +13,9 @@
interface LoggableInterface
{
function setLogger( \CLIFramework\Logger $logger );
function getLogger();

// we use __call magic to call logger
// function info($msg,$level = 0);
// function info2($msg,$level = 0);
}

29 changes: 29 additions & 0 deletions src/Onion/Package.php
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Onion package.
*
* (c) Yo-An Lin <cornelius.howl@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace Onion;

class Package
{

public $name;
public $version;
public $desc;

/**
* ConfigContainer object
*/
public $config;


}



103 changes: 96 additions & 7 deletions src/Onion/PackageConfigReader.php
Expand Up @@ -36,7 +36,97 @@
* $pkgxml->generate('package.xml');
*
*/
class PackageConfigReader
class PackageConfigReader
implements LoggableInterface
{
public $logger;

function __construct()
{
}

function setLogger( \CLIFramework\Logger $logger)
{
$this->logger = $logger;
}

function getLogger()
{
return $this->logger;
}

function __call($name,$arguments)
{
if( $this->logger )
call_user_func_array( array($this->logger,$name) , $arguments );
}

function read($file)
{
$logger = $this->getLogger();
$ini = null;
try {
$ini = parse_ini_file( $file , true );
}
catch( Exception $e ) {
throw new Exception( "Package.ini: $file syntax error: " . $e->getMessage() );
}

if( ! $ini )
throw new Exception( "$file is empty." );

$config = new ConfigContainer( $ini );

// preprocess, validate sections only for package.ini
$pkginfo = new Package;
$pkginfo->config = $config;

// validation
$requiredFields = explode(' ','package.name package.desc package.version');
foreach( $requiredFields as $f ) {
if( ! $config->has( $f ) )
throw new \Onion\Exception\InvalidConfigException( "$f is not defined." );
}

if( ! $config->has('package.authors') && ! $config->has('package.author') ) {
echo <<<EOT
Attribute 'author' or 'authors' is not defined.
Please define 'author' in your package.ini file:
[package]
author = Name <email@domain.com>
EOT;
throw new \Onion\Exception\InvalidConfigException('package.author or package.authors is not defined.');
}


// set default values
if( ! $config->has('package.summary') ) {
$logger->debug("* summary is not defined., use the first paragraph from description by default.",1);
$descs = explode("\n",$config->get('package.desc'));
$config->set('package.summary',$descs[0]); # use first line desc as summary by default.
}

if( ! $config->has('package.license') ) {
$logger->debug("* license is not defined., use PHP license by default.",1);
$config->set('package.license','PHP');
}

// build package meta info
$pkginfo->name = $config->get('package.name');
$pkginfo->desc = $config->get('package.desc');
$pkginfo->summary = $config->get('package.summary');
$pkginfo->version = $config->get('package.version');
$pkginfo->stability = $config->get('package.stability');



return $pkginfo;
}

}

class PackageConfigReader2
{
public $file;
public $config;
Expand Down Expand Up @@ -101,12 +191,6 @@ function readAsPackageXml()


/* check optional attributes */

if( ! $config->has('package.summary') ) {
$descs = explode("\n",$config->get('package.desc'));
$config->set('package.summary',$descs[0]); # use first line desc as summary by default.
}

if( ! $config->has('package.license') ) {
$logger->info2("* license is not defined., use PHP license by default.",1);
$config->set('package.license','PHP LICENSE');
Expand All @@ -121,6 +205,11 @@ function readAsPackageXml()
* <license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
*/



/**
* package xml must have some default value
*/
if( ! $config->has('package.channel' ) ) {
$logger->info2("* package channel is not defined. use pear.php.net by default.",1);
$config->set('package.channel','pear.php.net');
Expand Down
40 changes: 40 additions & 0 deletions tests/Onion/PackageConfigReaderTest.php
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Onion package.
*
* (c) Yo-An Lin <cornelius.howl@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace Onion;
use PHPUnit_Framework_TestCase;
class PackageConfigReaderTest extends PHPUnit_Framework_TestCase
{
function test()
{
$logger = new \CLIFramework\Logger;
ok( $logger );

$reader = new PackageConfigReader;
$reader->setLogger( $logger );

ob_start();
$reader->info( 'msg' );
$reader->error( 'msg' );
$reader->info2( 'msg' );
ob_end_clean();

ok( $reader );

ob_start();
$pkg = $reader->read( 'tests/data/package.ini' );
ob_end_clean();

ok( $pkg );


}
}

50 changes: 50 additions & 0 deletions tests/data/package.ini
@@ -0,0 +1,50 @@
[package]
name = Onion
summary = A Simple PHP Packager Builder.
desc = "Onion, The fast approch to make packages for PHP. Onion is
able to generate a PEAR-compatible package.xml file from a very simple config file."
version = 0.0.8
stability = alpha

; homepage = http://php-onion.org ; optional
; license = PHP ; optional, default to PHP
; version.api = 0.0.1 ; optional, defualt to "version"
; author = Yo-An Lin <cornelius.howl@gmail.com> # also works
author = Yo-An Lin <cornelius.howl@gmail.com>
authors[] = Yo-An Lin <cornelius.howl@gmail.com>
channel = pear.corneltek.com

[require]
php = 5.3
pearinstaller = 1.4.1

; packages
pear.corneltek.com/GetOptionKit = 0.0.2
pear.corneltek.com/CLIFramework = 0.0.2
pear.corneltek.com/Universal = 0.0.2

Test = resource


; extensions
; extension/reflection = 0
; extension/ctype = 0
extension/pcre = 0

[resource UrlLibrary]
type = library
url = http://foo.com/path/to/lib.tgz
library = src
autoload = PSR-0

[resource CLIFramework]
type = pear
git = http://github.com/c9s/CLIFramework.git

[roles]
onion.phar = script
changes.* = doc
*.md = doc

[repository]
git = git://github.com/c9s/Onion.git

0 comments on commit dd39afb

Please sign in to comment.