openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

XSDGenerator.php (2696B)


      1 <?php
      2 /**
      3  * Using the Component classes and generating a XSD-File.
      4  */
      5 
      6 use util\FileUtils;
      7 
      8 require(__DIR__.'/../../util/FileUtils.class.php');
      9 
     10 // Baseclass of all components.
     11 require('html/Component.class.php');
     12 require('html/HtmlComponent.class.php');
     13 require('html/FieldComponent.class.php');
     14 
     15 echo "XSD Generator\n\n";
     16 
     17 $folder = FileUtils::readDir(__DIR__ . '/html');
     18 
     19 $componentsFile = @fopen(__DIR__ . '/components.ini', 'w');
     20 $xsdFile = @fopen(__DIR__ . '/template.xsd', 'w');
     21 
     22 fwrite($componentsFile, '# generated by XSDGenerator. do not change manually.' . "\n");
     23 fwrite($xsdFile, '<?xml version="1.0" encoding="utf-8"?>
     24 <xsd:schema xmlns="http://www.openrat.de/template"
     25 	targetNamespace="http://www.openrat.de/template" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     26 	elementFormDefault="qualified" attributeFormDefault="unqualified">' . "\n".
     27 	'<!-- generated by XSDGenerator. do not change manually -->'."\n");
     28 
     29 // Only directories
     30 $folder = array_filter( $folder,function($name) {
     31 	return substr($name,0,10) == 'component_';
     32 } );
     33 // Cut 'component_' from directory names
     34 $folder = array_map( function($name) {
     35 	return substr($name,10);
     36 }, $folder );
     37 
     38 // Sort...
     39 asort($folder);
     40 
     41 foreach ($folder as $componentName) {
     42 
     43     echo 'Working on component: ' . $componentName . "\n";
     44     fwrite($componentsFile, $componentName . " = \n");
     45 
     46     fwrite($xsdFile, '<xsd:element name="' . $componentName . '" type="' . $componentName . 'Type" />'
     47 		.'<xsd:complexType name="' . $componentName . 'Type">');
     48 
     49     // Allow HTML children
     50 	//fwrite($xsdFile, '<xsd:openContent mode="interleave">'.
     51     //	'<xsd:any namespace="http://www.w3.org/1999/xhtml" />'.
     52 	//	'</xsd:openContent>');
     53 
     54     // Allowed Child-Elements (all)
     55     fwrite($xsdFile, '<xsd:choice maxOccurs="unbounded" minOccurs="0">');
     56     foreach ($folder as $cName) {
     57 		fwrite($xsdFile, '<xsd:element ref="' . $cName . '" maxOccurs="unbounded" minOccurs="0" />');
     58     }
     59 
     60 	fwrite($xsdFile,'<xsd:any namespace="http://www.w3.org/1999/xhtml" />' );
     61 
     62     fwrite($xsdFile, '</xsd:choice>');
     63 
     64 
     65     $className = 'template_engine\\components\\html\\component_'.$componentName.'\\' . ucfirst($componentName) . 'Component';
     66     $vars = get_class_vars($className);
     67     foreach ($vars as $name => $value) {
     68         if (is_bool($value))
     69             $xsdtype = 'xsd:boolean';
     70         elseif (is_numeric($value))
     71             $xsdtype = 'xsd:int';
     72         else
     73             $xsdtype = 'xsd:string';
     74 
     75 
     76         fwrite($xsdFile, '<xsd:attribute name="' . $name . '" type="' . $xsdtype . '" />');
     77     }
     78 
     79 
     80     fwrite($xsdFile, '</xsd:complexType>');
     81 
     82 }
     83 
     84 fwrite($xsdFile, '</xsd:schema>');
     85 
     86 fclose($xsdFile);
     87 fclose($componentsFile);
     88 
     89 echo "Finished.\n";