univ-pau-ics/build/service/Config.php

318 lines
7.9 KiB
PHP
Raw Normal View History

<?php
2017-09-13 13:03:36 +00:00
namespace service;
2017-09-14 17:56:24 +00:00
use \database\core\DatabaseDriver;
2017-09-13 13:03:36 +00:00
class Config{
/* [1] Attributes
=========================================================*/
private $diplomes = null;
private $periods = null;
private static function monthAsso(){
return [
'janvier' => '01',
'février' => '02',
'mars' => '03',
'avril' => '04',
'mai' => '05',
'juin' => '06',
'juillet' => '07',
'août' => '08',
'septembre' => '09',
'octobre' => '10',
'novembre' => '11',
'décembre' => '12'
];
}
/* (1) Constructs a filled config object
*
* @d_list<Array> List of the diplomes ( ID => name )
* @p_list<Array> List of the periods ( ID => first day's date )
*
* @return this<Config> New instance
*
---------------------------------------------------------*/
private function __construct($d_list=null, $p_list=null){
/* [1] Check arguments (type: array)
=========================================================*/
if( !is_array($d_list) || !is_array($p_list) )
throw new \Exception("Config.__construct(<Array>, <Array) expected but Config.__construct(<".gettype($d_list).">, <".gettype($d_list).">) received");
/* [2] Basic setter
=========================================================*/
/* (1) Set the diplomes' list */
$this->diplomes = $d_list;
/* (2) Set the periods' list */
$this->periods = $p_list;
}
/* (2) Loads the configuration from URL
*
* @d_script<String> URL of the script containing the diplomes list
* @p_script<String> URL of the script containing the periods list
*
* @return instance<Config> The created instance
* NULL on error
*
---------------------------------------------------------*/
public static function load($d_script=null, $p_script=null){
/* [1] Check arguments (type + validity)
=========================================================*/ {
/* (1) Check arguments' types (string, string) */
if( !is_string($d_script) || !is_string($p_script) )
throw new \Exception("Config::load(<Array>, <Array) expected but Config::load(<".gettype($d_script).">, <".gettype($d_script).">) received");
/* (2) Check @d_script URL validity */
if( !($d_h=@get_headers($d_script)) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
/* (3) Check @p_script URL validity */
if( !($p_h=@get_headers($p_script)) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
/* (4) Check image exists @dscript */
if( !preg_match('@HTTP.+200@', $d_h[0]) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
/* (5) Check image exists @dscript */
if( !preg_match('@HTTP.+200@', $p_h[0]) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
}
/* [2] Get file cursors
=========================================================*/ {
/* (1) Try to get file cursors */
try{
$d_cur = new \SplFileObject($d_script, "r");
$p_cur = new \SplFileObject($p_script, "r");
/* (2) Raise on failure */
}catch(\Exception $e){
throw new \Exception("Config::load(<1>, <2>) cannot get a cursor on a one of both files");
}
}
/* [3] Filter and register content
=========================================================*/{
/* (1) Manage diplomes
---------------------------------------------------------*/ {
/* (1) Initialize diplomes' list */
$d_list = [];
/* (2) Fetch each line and extract data */
while( !$d_cur->eof() ){
// {1} If does not match -> go to next line //
if( !preg_match('@"grDiplome","(?:&lt;)?(.+)(?:&gt;)?","T(.+)"@i', $d_cur->fgets(), $m) )
continue;
// {2} Register into diplome list //
$d_list[ "T${m[2]}" ] = html_entity_decode($m[1]);
}
/* (2) Close file cursor */
$d_cur = null;
}
/* (2) Manage periods
---------------------------------------------------------*/{
/* (1) Initialize periods' list */
$p_list = [];
/* (2) Fetch each line and extract data */
while( !$p_cur->eof() ){
// {1} If does not match -> go to next line //
if( !preg_match('@"T\d+","du \d+ au (\d+) ([a-z]+) (\d+)","T\d+S(.+)"@i', $p_cur->fgets(), $m) )
continue;
// {2} Get numeric month number //
$month = self::monthAsso()[ $m[2] ];
// {3} Get monday date //
$mon = date( 'd-m-Y', strtotime("${m[1]}-$month-${m[3]} - 5 days") );
// {4} Register into the list //
$p_list[ "S${m[4]}" ] = $mon;
}
/* (2) Close file cursor */
$p_cur = null;
}
}
2017-09-13 13:03:36 +00:00
/* [4] Cache into light-database
=========================================================*/ {
2017-09-14 17:56:24 +00:00
/* (1) Get diplome ids */
$sqlr = DatabaseDriver::getPDO()->query("SELECT id_diplome from diplome");
$db_ids_raw = $sqlr->fetchAll();
/* (2) Format data */
$db_ids = [];
foreach($db_ids_raw as $data)
$db_ids[$data['id_diplome']] = null;
/* (3) For each diplome */
foreach($d_list as $id=>$name){
// {1} Add if not in db //
if( !isset($db_ids[$id]) ){
$sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO `diplome`(`id_diplome`, `name`, `updated_at`) VALUES(:id, :name, DEFAULT)");
$sqlr->execute([ ':id' => $id, ':name' => $name ]);
// {2} If update //
}else{
$sqlr = DatabaseDriver::getPDO()->prepare("UPDATE diplome SET name = :name, updated_at = :upd WHERE id_diplome = :id");
$sqlr->execute([ ':id' => $id, ':name' => $name, 'upd' => gmmktime() ]);
}
2017-09-13 13:03:36 +00:00
}
2017-09-14 17:56:24 +00:00
/* (4) Get period ids */
$sqlr = DatabaseDriver::getPDO()->query("SELECT id_period from period");
$db_ids_raw = $sqlr->fetchAll();
/* (5) Format data */
$db_ids = [];
foreach($db_ids_raw as $data)
$db_ids[$data['id_period']] = null;
/* (6) For each period */
foreach($p_list as $id=>$monday){
2017-09-14 17:56:24 +00:00
// {1} Add if not in db //
if( !isset($db_ids[$id]) ){
$sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO `period`(`id_period`, `monday`) VALUES(:id, :monday)");
$sqlr->execute([ ':id' => $id, ':monday' => $monday ]);
// {2} If update //
}else{
$sqlr = DatabaseDriver::getPDO()->prepare("UPDATE period SET monday = :monday WHERE id_period = :id");
$sqlr->execute([ ':id' => $id, ':monday' => $monday ]);
}
}
2017-09-13 13:03:36 +00:00
}
/* [x] Return the instance */
return new Config($d_list, $p_list);
}
/* (3) Loads the configuration from URL
*
* @return instance<Config> The created instance
* NULL on error
*
---------------------------------------------------------*/
public static function loadCached(){
/* [1] Load cache
=========================================================*/ {
2017-09-14 17:56:24 +00:00
/* (1) Get diplome list */
$d_list_raw = DatabaseDriver::getPDO()->query("SELECT * FROM diplome")->fetchAll();
2017-09-14 17:56:24 +00:00
/* (2) Format data */
$d_list = [];
foreach($d_list_raw as $data)
$d_list[$data['id_diplome']] = [ $data['name'], $data['updated_at'] ];
/* (1) Get diplome list */
$p_list_raw = DatabaseDriver::getPDO()->query("SELECT * FROM period")->fetchAll();
/* (2) Format data */
$p_list = [];
2017-09-14 17:56:24 +00:00
foreach($p_list_raw as $data)
$p_list[$data['id_period']] = $data['monday'];
}
/* [2] Return the instance
=========================================================*/
return new Config($d_list, $p_list);
}
/* (4) Getter diplomes
*
* @return diplomes<Array> Diplome list
*
---------------------------------------------------------*/
public function getDiplomes(){
return $this->diplomes;
}
/* (5) Getter periods
*
* @return periods<Array> Period list
*
---------------------------------------------------------*/
public function getPeriods(){
return $this->periods;
}
}