Advertisement
Guest User

Loader 15-06

a guest
Jun 15th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2. namespace Shinoa\StudentsList;
  3.  
  4.  
  5. use Shinoa\StudentsList\Exceptions\LoaderException;
  6.  
  7. class Loader
  8. {
  9.     //единственный экземпляр класса
  10.     private static $root;
  11.     private static $instance;
  12.     private static $config;
  13.     private static $dsn;
  14.     private static $pdo;
  15.    
  16.     /**
  17.      * Unreachable constructor.
  18.      */
  19.     private function __construct()
  20.     {
  21.     }
  22.    
  23.     /**
  24.      * @param mixed $root
  25.      */
  26.     public static function setRoot($root)
  27.     {
  28.         self::$root = $root;
  29.     }
  30.    
  31.     /**
  32.      * @return mixed
  33.      */
  34.     public static function getRoot()
  35.     {
  36.         if (isset(self::$root)) {
  37.             return self::$root;
  38.         } else throw new LoaderException('Cannot get root: root is not defined.');
  39.     }
  40.    
  41.     /**
  42.      * @return mixed
  43.      */
  44.     public static function getConfig()
  45.     {
  46.         $configFactory = function ($root) {
  47.             if (file_exists(appendFilePath([$root, 'Students', 'ini', 'config.xml']))) {
  48.                 $configPath = appendFilePath([$root, 'Students', 'ini', 'config.xml']);
  49.             } elseif (file_exists(appendFilePath([$root, 'Students', 'ini', 'config_test.xml']))) {
  50.                 $configPath = appendFilePath([$root, 'Students', 'ini', 'config_test.xml']);
  51.             } else {
  52.                 throw new LoaderException('Cannot load config!');
  53.             }
  54.             return simplexml_load_file($configPath);
  55.            
  56.         };
  57.        
  58.         if ( !isset(self::$config) ) {
  59.             $root = self::getRoot();
  60.             if ( isset($root) ) {
  61.                 self::$config = $configFactory(self::$root);
  62.             } else throw new LoaderException('Failed to create config: root is not defined.');
  63.         }
  64.         return self::$config;
  65.     }
  66.    
  67.     public static function getDSN()
  68.     {
  69.         $dsnFactory = function ($config) {
  70.             if (empty($config)) {
  71.                 throw new LoaderException('Configuration is not properly loaded');
  72.             }
  73.             $dsn = "mysql:host=localhost;dbname={$config->database->dbname};charset=utf8";
  74.             return $dsn;
  75.         };
  76.        
  77.         if ( !isset(self::$config) ) {
  78.             if ( isset(self::$config) ) {
  79.                 self::$dsn = $dsnFactory(self::$root);
  80.             } else throw new LoaderException('Failed to create dsn: root is not defined.');
  81.         }
  82.         return self::$config;
  83.     }
  84.    
  85.     public static function getPDO()
  86.     {
  87.         $pdoFactory = function ($config, $dsn) {
  88.             //загружаем данные для соединения
  89.             if (!isset($config->database->username)
  90.                 ||
  91.                 !isset($config->database->password)
  92.             ) {
  93.                 throw new LoaderException('Config  not loaded or empty');
  94.             } else {
  95.                 $username = $config->database->username;
  96.                 $password = $config->database->password;
  97.             }
  98.             //пробуем соединиться
  99.             try {
  100.                 $opt = array(
  101.                     \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  102.                     \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  103.                     \PDO::ATTR_EMULATE_PREPARES => false,
  104.                     \PDO::MYSQL_ATTR_FOUND_ROWS => true
  105.                 );
  106.                 $pdo = new \PDO($dsn, $username, $password, $opt);
  107.             } catch (\PDOException $e) {
  108.                 throw new LoaderException('Ошибка при подключении к базе данных');
  109.             }
  110.             return $pdo;
  111.         };
  112.        
  113.         if ( !isset(self::$pdo)) {
  114.             if ( isset(self::$config) || isset(self::$dsn)) {
  115.                 self::$pdo = $pdoFactory(self::$config, self::$dsn);
  116.             } else throw new LoaderException('Failed to create dsn: root is not defined.');
  117.         }
  118.         return self::$pdo;
  119.     }
  120.    
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement