<?php
class DBgeneration{
var $_sPath_projects = '';
var $_oProjet = NULL;
var $_oObjects = NULL;
function DBgeneration( $oProjets, $oObjects ){
$this->_sPath_projects = PATH_BASE.'projects'.DIRECTORY_SEPARATOR;
$this->_oProjets = $oProjets;
$this->_oObjects = $oObjects;
}
//generation du projet
function generation( $sProjet, $bOpt_genere_class_main, $bOpt_clean_folder ){
$sResult = '';
global $oTemplate;
$oData = $this->_oProjets->getData( $sProjet );
//si il existe un repertoire de sortie valie
if( is_dir( $oData->outputPath ) )
$sDirProjet = $oData->outputPath;
else
$sDirProjet = $this->_sPath_projects.$sProjet.DIRECTORY_SEPARATOR.'generate'.DIRECTORY_SEPARATOR;
$sDirProjet = str_replace( '/', DIRECTORY_SEPARATOR, $sDirProjet );
$sDirProjet = str_replace( '\\', DIRECTORY_SEPARATOR, $sDirProjet );
$sDirProjet = rtrim( $sDirProjet, DIRECTORY_SEPARATOR ).DIRECTORY_SEPARATOR;
//supprime les donnees existantes
if( $bOpt_clean_folder && is_dir( $sDirProjet ) ){
$this->delDir( $sDirProjet );
}
if( !is_dir( $sDirProjet ) ){
mkdir( $sDirProjet );
}
$sDirObjects = $sDirProjet.'objects'.DIRECTORY_SEPARATOR;
if( !file_exists( $sDirObjects ) ){
mkdir( $sDirObjects );
}
$sDirCollections = $sDirProjet.'collections'.DIRECTORY_SEPARATOR;
if( !file_exists( $sDirCollections ) ){
mkdir( $sDirCollections );
}
//creation du fichier de base
$sData = '';
if( $bOpt_genere_class_main ){
include( 'templates-files'.DIRECTORY_SEPARATOR.'phpsimpledb.class.php' );
$sResult .= 'creation du fichier de base : phpsimpledb.class.php'."\n";
//ecrit le fichier phpsimpledb.class.php
file_put_contents( $sDirProjet.'phpsimpledb.class.php', $sData );
$sResult .= 'ecrit le fichier phpsimpledb.class.php'."\n";
}
//copie de l'extends des objets
copy( 'templates-files'.DIRECTORY_SEPARATOR.'extends.objects.class.php', $sDirProjet.'objects'.DIRECTORY_SEPARATOR.'extends.objects.class.php' );
$sResult .= 'copie de l\'extends des objets'."\n";
//creation des classes pour les objets
$oObjects = $this->_oObjects->getObjects( $sProjet );
$sResult .= 'creation des classes pour les objets'."\n";
foreach( $oObjects as $oObject ){
include( 'templates-files'.DIRECTORY_SEPARATOR.'object.class.php' );
//ecrit le fichier de l'objet
file_put_contents( $sDirObjects.$oObject->sName.'.class.php', $sData );
$sResult .= '- ecrit le fichier de l\'objet : '.$oObject->sName.'.class.php'."\n";
}
//copie de l'extends des collections
copy( 'templates-files'.DIRECTORY_SEPARATOR.'extends.collections.class.php', $sDirProjet.'collections'.DIRECTORY_SEPARATOR.'extends.collections.class.php' );
$sResult .= 'copie de l\'extends des collections'."\n";
//creation des collections pour les objets
$sResult .= 'creation des collections pour les objets'."\n";
foreach( $oObjects as $oObject ){
include( 'templates-files'.DIRECTORY_SEPARATOR.'collection.class.php' );
//ecrit le fichier de collection
file_put_contents( $sDirCollections.$oObject->sName.'.class.php', $sData );
$sResult .= '- ecrit le fichier de collection : '.$oObject->sName.'.class.php'."\n";
}
return $sResult;
}
//supprime un repertoire
function delDir( $dirName ){
if( empty( $dirName ) )
return true;
if(file_exists( $dirName )){
$dir = dir( $dirName );
while( $file = $dir->read() )
if( $file != '.' && $file != '..' ){
if( is_dir( $dirName.DIRECTORY_SEPARATOR.$file ) )
$this->delDir( $dirName.DIRECTORY_SEPARATOR.$file );
else
unlink( $dirName.DIRECTORY_SEPARATOR.$file );
}
$dir->close();
rmdir( $dirName );
}
return true;
}
}
?>