Hey guys,
Some patches to improve the factory system and load the formats dynamically.
what do you think?
--Moacir
Index: phpdotnet/phd/BuildOptionsParser.php
===================================================================
--- phpdotnet/phd/BuildOptionsParser.php (revision 284053)
+++ phpdotnet/phd/BuildOptionsParser.php (working copy)
@@ -36,21 +36,8 @@
{
$formats = array();
foreach((array)$v as $i => $val) {
- switch($val) {
- case "xhtml":
- case "bigxhtml":
- case "howto":
- case "php":
- case "manpage":
- case "kdevelop":
- case "pdf":
- case "bigpdf":
- if (!in_array($val, $formats)) {
- $formats[] = $val;
- }
- break;
- default:
- trigger_error("Format not supported at this time",
E_USER_ERROR);
+ if (!in_array($val, $formats)) {
+ $formats[] = $val;
}
}
Config::set_output_format($formats);
@@ -139,7 +126,7 @@
if (is_null($packageList)) {
$packageList = array();
foreach (glob($GLOBALS['ROOT'] . "/phpdotnet/phd/Package/*",
GLOB_ONLYDIR) as $item) {
- if (!in_array(basename($item), array('CVS', '.', '..'))) {
+ if (!in_array(basename($item), array('.svn', '.', '..'))) {
$packageList[] = basename($item);
}
}
@@ -223,21 +210,16 @@
if (is_null($packageList)) {
$packageList = array();
foreach (glob($GLOBALS['ROOT'] . "/phpdotnet/phd/Package/*",
GLOB_ONLYDIR) as $item) {
- if (!in_array(basename($item), array('CVS', '.', '..'))) {
- $formats = array();
- foreach (glob($item . "/*.php") as $subitem) {
- if (strcmp(basename($subitem), "Factory.php") != 0) {
- $formats[] = substr(basename($subitem), 0, -4);
- }
- }
- $packageList[basename($item)] = $formats;
+ if (!in_array(basename($item), array('.svn', '.', '..'))) {
+ $packageList[] = basename($item);
}
}
}
echo "Supported packages:\n";
- foreach ($packageList as $package => $formats) {
- echo "\t" . $package . "\n\t\t" . implode("\n\t\t", $formats) .
"\n";
+ foreach ($packageList as $package) {
+ $factory = Format_Factory::createFactory($package);
+ echo "\t" . $package . "\n\t\t" . implode("\n\t\t",
$factory->getOutputFormats()) . "\n";
}
exit(0);
Index: phpdotnet/phd/Config.php
===================================================================
--- phpdotnet/phd/Config.php (revision 284053)
+++ phpdotnet/phd/Config.php (working copy)
@@ -7,16 +7,7 @@
class Config
{
private static $optionArray = array(
- 'output_format' => array(
- 'xhtml',
- 'php',
- 'bigxhtml',
- 'howto',
- 'manpage',
- 'kdevelop',
- 'pdf',
- 'bigpdf',
- ),
+ 'output_format' => array(),
'chunk_extra' => array(
"legalnotice" => true,
"phpdoc:exception" => true,
Index: ../phpdotnet/phd/Package/Default/Factory.php
===================================================================
--- ../phpdotnet/phd/Package/Default/Factory.php (revision 283894)
+++ ../phpdotnet/phd/Package/Default/Factory.php (working copy)
@@ -2,18 +2,15 @@
namespace phpdotnet\phd;
class Package_Default_Factory extends Format_Factory {
- public function createXhtmlFormat() {
- return new Package_Default_ChunkedXHTML();
- }
-
- public function createBigXhtmlFormat() {
- return new Package_Default_BigXHTML();
- }
-
- public function createPHPFormat() {
- return new Package_Default_PHP();
- }
+ private $formats = array(
+ 'xhtml' => 'Package_Default_ChunkedXHTML',
+ 'bigxhtml' => 'Package_Default_BigXHTML',
+ 'php' => 'Package_Default_PHP',
+ );
+ public function __construct() {
+ parent::registerOutputFormats($this->formats);
+ }
}
?>
Index: phpdotnet/phd/Format/Factory.php
===================================================================
--- phpdotnet/phd/Format/Factory.php (revision 284054)
+++ phpdotnet/phd/Format/Factory.php (working copy)
@@ -1,36 +1,34 @@
<?php
namespace phpdotnet\phd;
-abstract class Format_Factory
-{
- public function createXhtmlFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
+abstract class Format_Factory {
+ private $formats = array();
+
+ public final function getOutputFormats() {
+ return array_keys($this->formats);
}
- public function createBigXhtmlFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
+
+ public final function registerOutputFormats($formats) {
+ $this->formats = $formats;
}
- public function createPHPFormat() {
+
+ public final function createFormat($format) {
+ if (isset($this->formats[$format]) && $this->formats[$format]) {
+ $classname = __NAMESPACE__ . "\\" . $this->formats[$format];
+
+ $obj = new $classname();
+ if (!($obj instanceof Format)) {
+ throw new \Exception("All Formats must inherit Format");
+ }
+ return $obj;
+ }
trigger_error("This format is not supported by this package",
E_USER_ERROR);
}
- public function createHowToFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
- }
- public function createManpageFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
- }
- public function createKDevelopFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
- }
- public function createPDFFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
- }
- public function createBigPDFFormat() {
- trigger_error("This format is not supported by this package",
E_USER_ERROR);
- }
- public static final function createFactory()
- {
- $package = Config::package();
+ public static final function createFactory($package = null) {
+ if ($package === null) {
+ $package = Config::package();
+ }
$classname = __NAMESPACE__ . "\\Package_" . $package . "_Factory";
$factory = new $classname();
Index: ../phpdotnet/phd/Package/Pear/Factory.php
===================================================================
--- ../phpdotnet/phd/Package/Pear/Factory.php (revision 283894)
+++ ../phpdotnet/phd/Package/Pear/Factory.php (working copy)
@@ -2,22 +2,15 @@
namespace phpdotnet\phd;
class Package_Pear_Factory extends Format_Factory {
- public function createBigXhtmlFormat() {
- return new Package_Pear_BigXHTML();
- }
-
- public function createXhtmlFormat() {
- return new Package_Pear_ChunkedXHTML();
- }
-
- public function createPHPFormat() {
- return new Package_Pear_Web();
- }
+ private $formats = array(
+ 'xhtml' => 'Package_Pear_ChunkedXHTML',
+ 'bigxhtml' => 'Package_Pear_BigXHTML',
+ 'php' => 'Package_Pear_Web',
+ );
- public function createChmFormat() {
- return new Package_Pear_CHM();
+ public function __construct() {
+ parent::registerOutputFormats($this->formats);
}
-
}
?>
Index: phpdotnet/phd/Package/PHP/Factory.php
===================================================================
--- phpdotnet/phd/Package/PHP/Factory.php (revision 284056)
+++ phpdotnet/phd/Package/PHP/Factory.php (working copy)
@@ -2,43 +2,20 @@
namespace phpdotnet\phd;
class Package_PHP_Factory extends Format_Factory {
-
- public function createBigXhtmlFormat() {
- return new Package_PHP_BigXHTML();
- }
-
- public function createXhtmlFormat() {
- return new Package_PHP_ChunkedXHTML();
+ private $formats = array(
+ 'xhtml' => 'Package_PHP_ChunkedXHTML',
+ 'bigxhtml' => 'Package_PHP_BigXHTML',
+ 'php' => 'Package_PHP_Web',
+ 'howto' => 'Package_PHP_HowTo',
+ 'manpage' => 'Package_PHP_Functions',
+ 'pdf' => 'Package_PHP_PDF',
+ 'bigpdf' => 'Package_PHP_BigPDF',
+ 'kdevelop' => 'Package_PHP_KDevelop',
+ );
+
+ public function __construct() {
+ parent::registerOutputFormats($this->formats);
}
-
- public function createPHPFormat() {
- return new Package_PHP_Web();
- }
-
- public function createHowToFormat() {
- return new Package_PHP_HowTo();
- }
-
- public function createManpageFormat() {
- return new Package_PHP_Functions();
- }
-
- public function createKDevelopFormat() {
- return new Package_PHP_KDevelop();
- }
-
- public function createPDFFormat() {
- return new Package_PHP_PDF();
- }
-
- public function createBigPDFFormat() {
- return new Package_PHP_BigPDF();
- }
-/*
- public function createChmFormat() {
- return new Package_PHP_CHM();
- }
-*/
}
?>
Index: render.php
===================================================================
--- render.php (revision 284053)
+++ render.php (working copy)
@@ -63,33 +63,12 @@
v("Skipping indexing", VERBOSE_INDEXING);
}
+ if (count(Config::output_format()) == 0) {
+ Config::set_output_format($factory->getOutputFormats());
+ }
+
foreach (Config::output_format() as $format) {
- switch($format) {
- case "xhtml": // Standalone Chunked xHTML Format
- $render->attach($factory->createXhtmlFormat());
- break;
- case "php": // Standalone phpweb Format
- $render->attach($factory->createPHPFormat());
- break;
- case "bigxhtml": // Standalone Big xHTML Format
- $render->attach($factory->createBigXhtmlFormat());
- break;
- case "howto": // Standalone HowTo Format
- $render->attach($factory->createHowToFormat());
- break;
- case "manpage": // Standalone Manpage Format
- $render->attach($factory->createManpageFormat());
- break;
- case "kdevelop": // Standalone KDevelop Format
- $render->attach($factory->createKDevelopFormat());
- break;
- case "pdf": // Standalone PDF Format
- $render->attach($factory->createPDFFormat());
- break;
- case "bigpdf": // Standalone BigPDF Format
- $render->attach($factory->createBigPDFFormat());
- break;
- }
+ $render->attach($factory->createFormat($format));
}
}