Einzelnen Beitrag anzeigen
  #5  
Alt 15.04.2008, 19:58:56
Benutzerbild von Socrates
Socrates Socrates ist offline
Member
 
Registriert seit: Jul 2006
Ort: Göttingen/Deutschland
Alter: 35
Beiträge: 586
AW: Aufbau Repository Klasse

Abend!
Sieht doch ganz gut aus. Vorallem das "Fertsch" am Ende gefällt mir besonders gut. :-)

Da ich heute, wie ich zu wissen glaube, erfolgreich Politik bestanden habe und so nach Deutsch die zweite, hoffentlich gute Arbeit hinter mir liegt, dachte ich mir ich könnte mir einige Zeilen Code gönnen. Deshalb habe ich ein kleines Bisschen meiner Zeit damit verbracht, einige Zeilen an meiner Repository-Klasse zu schreiben. Ist wahrscheinlich noch nicht alles so optimal, aber ich hoffe, dass ihr mir vielleicht noch etwas helfen könnt.

Zum besseren Verständnis der Klasse sei gesagt, dass die Datei, die die URLs zu den Repository-Servern enthält, nun repos.csv heißt. Sie ist wie folgt aufgebaut:

Öffentlich zugängliche Packages
Code:
repos http://www.host.de/path/to/repos/dir/ public
Kostenpflichtige Packages
Code:
repos http://www.host.de/path/to/repos/dir/ privat 234f
Wie ihr seht steht am Anfang jeder Eintagrung repos gefolgt von einer url. Das nachfolgende public oder privat bestimmt, ob ein Repository-Server gesichert ist oder nicht. Wenn privat steht, folgt ein vierstelliges Passwort.

Die Datei control.csv, die die Package Informationen enthält, sieht aus, wie oben beschrieben. Die einzelnen Info-Blöcke sind durch Leerzeilen getrennt. Die Klasse findet ihr im momentanen Zustand angehängt. Vorschläge und Anregungen sind gerne gesehen. Besondere Hilfe bedürfte ich auch noch bei der getPackInfo()-Methode. Kleine Blockade! :-(

Repository.php
PHP-Code:
<pre>

<?php  
error_reporting
(E_ALL);
    
// define server-constants
define ('REPOS'0);
define ('SERVER'1);
define ('PERM'2);
define ('PWD'3);
    
// define constants for control.csv
define ('PACK'0);
define ('VERSION'1);
define ('DEPENDS'2);
define ('SUGGESTS'3);
define ('SIZE'4);
define ('AUTHOR'5);
define ('DESCRIPTION'6);

/***
* The Repository class allows reading files that hold the repository-servers,
* reading information about different packages and
* downloading the spezific packages
*  
* @package Repository
* @version 1.0
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/  
class Repository
{
    
// protected class variables
    
protected $repos = array();
    protected 
$packages = array();

    
/**
    * Constructor - Is called when the class is instanced
    *
    * @access: public
    * @param Str $path
    * @return NONE
    */
    
public function __construct($path 'repos.csv')
    {    
        
// save each line of data to an array-element
        
$lines file($path);
        
        foreach(
$lines as $server)
        {
            
// split up the differnet elements of a line by delimeter " "
            
$server preg_split("/ /"$server100PREG_SPLIT_NO_EMPTY);
            
            
print_r($server);
            
            
// is it a file that contains repository-servers?
            
if( $server[REPOS] == 'repos' )
            {    
                if( 
$server[PERM] == 'public' )
                {
                    
$this->repos[$server[SERVER]] = 'NONE';
                }
                
                else if( 
$server[PERM] == 'privat' )
                {
                    
$this->repos[$server[SERVER]] = $server[PWD];
                }
                
                else
                {
                    throw new 
Exception("$path is not a valid path to repository-server.");
                }                    
            }
            
            else
            {
                throw new 
Exception("$path holds no repository-servers."); 
            }
        }
    }
    
    
/**
    * getRepos() - Returns the array of repository-servers and passwords
    *
    * @access: public
    * @return Array
    */
    
public function getRepos()
    {
        return 
$this->repos;
    }
    
    
/**
    * getPackInfo() - Gets the information about the different packages
    *
    * @access: public
    * @return Array
    */
    
public function getPackInfo()
    {
        
// get repository-servers
        
$host $this->getRepos();
        
        foreach( 
$host as $server => $pwd)
        {        
            
// path to the file that contains the package-information
            
$path 'control.csv';
            
            
// save each line of data to an array-element
            
$lines file($server.$path);
            
            
// split up the control.csv into the different information
            
$block split("\n\n"$lines);
            
            
// save information to an array with the last element as url to the package
            
foreach($block as $pack)
            {
                
// extract each line of the information-block
                
$line split("\n"$pack);
                
                
// delete description at the beginning of each line
                
preg_replace '/^package: /'' '$line[PACK]);
                
preg_replace '/^version: /'' '$line[VERSION]);
                
preg_replace '/^depends: /'' '$line[DEPENDS]);
                
preg_replace '/^suggests: /'' '$line[SUGGESTS]);
                
preg_replace '/^size: /'' '$line[SIZE]);
                
preg_replace '/^author: /'' '$line[AUTHOR]);
                
preg_replace '/^desciption: /'' '$line[DESCRIPTION]);
            
                
// save information to array
                
$this->packages[]["package"] = $line[PACK];
                
$this->packages[]["version"] = $line[VERSION];
                
$this->packages[]["depends"] = $line[DEPENDS];
                
$this->packages[]["suggests"] = $line[SUGGESTS];
                
$this->packages[]["size"] = $line[SIZE];
                
$this->packages[]["author"] = $line[AUTHOR];
                
$this->packages[]["desciption"] = $line[DESCRIPTION];
                
$this->packages[]["url"] = $server.$line[PACK];
            }
        }
        
        return 
$this->packages;
    }
    
    
/**
    * getPack() - Downloads a package to your server
    *
    * @access: public
    * @param Str $path
    * @return NONE
    */
    
public function getPack($path '')
    {
    }
}

try {
    
$repos = new Repository('repos.txt');
    
$server $repos->getRepos();
    
$packs $repos->getPackInfo();
    
    
print_r ($server);
    
print_r ($packs);    
}

catch(
Exception $e) {
    echo 
$e->getMessage();
}

?>

</pre>
Die Methode getPack() wird, wie von dir |Coding vorgeschlagen, vorraussichtlich auf cURL basieren. Nichts genaues weiß man aber noch nicht. XD
MfG, Andy
__________________
BSc. Applied Computer Science
http://www.bornageek.com

Geändert von Socrates (18.04.2008 um 09:13:01 Uhr)
Mit Zitat antworten