| @@ 4-65 (lines=62) @@ | ||
| 1 | <?php |
|
| 2 | namespace FMUP\Cache; |
|
| 3 | ||
| 4 | class Factory |
|
| 5 | { |
|
| 6 | const DRIVER_RAM = 'Ram'; |
|
| 7 | const DRIVER_FILE = 'File'; |
|
| 8 | const DRIVER_SHM = 'Shm'; |
|
| 9 | private static $instance; |
|
| 10 | ||
| 11 | /** |
|
| 12 | * @codeCoverageIgnore |
|
| 13 | */ |
|
| 14 | private function __construct() |
|
| 15 | { |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @codeCoverageIgnore |
|
| 20 | */ |
|
| 21 | private function __clone() |
|
| 22 | { |
|
| 23 | } |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @return self |
|
| 27 | */ |
|
| 28 | final public static function getInstance() |
|
| 29 | { |
|
| 30 | if (!self::$instance) { |
|
| 31 | $class = get_called_class(); |
|
| 32 | self::$instance = new $class(); |
|
| 33 | } |
|
| 34 | return self::$instance; |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @param string $driver |
|
| 39 | * @param array $params |
|
| 40 | * @return CacheInterface |
|
| 41 | * @throws Exception |
|
| 42 | */ |
|
| 43 | final public function create($driver = self::DRIVER_RAM, $params = array()) |
|
| 44 | { |
|
| 45 | $class = $this->getClassForName($driver); |
|
| 46 | if (!class_exists($class)) { |
|
| 47 | throw new Exception('Unable to create ' . $class); |
|
| 48 | } |
|
| 49 | $instance = new $class($params); |
|
| 50 | if (!$instance instanceof CacheInterface) { |
|
| 51 | throw new Exception('Unable to create ' . $class); |
|
| 52 | } |
|
| 53 | return $instance; |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * Must return full class name for specified driver name |
|
| 58 | * @param string $driver |
|
| 59 | * @return string |
|
| 60 | */ |
|
| 61 | protected function getClassForName($driver) |
|
| 62 | { |
|
| 63 | return __NAMESPACE__ . '\Driver\\' . ucfirst($driver); |
|
| 64 | } |
|
| 65 | } |
|
| 66 | ||
| @@ 4-64 (lines=61) @@ | ||
| 1 | <?php |
|
| 2 | namespace FMUP\Ftp; |
|
| 3 | ||
| 4 | class Factory |
|
| 5 | { |
|
| 6 | const DRIVER_FTP = 'Ftp'; |
|
| 7 | const DRIVER_SFTP = 'Sftp'; |
|
| 8 | const DRIVER_FTP_IMPLICIT_SSL = 'FtpImplicitSSL'; |
|
| 9 | ||
| 10 | private static $instance; |
|
| 11 | ||
| 12 | private function __construct() |
|
| 13 | { |
|
| 14 | } |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Design pattern Singleton |
|
| 18 | * @codeCoverageIgnore |
|
| 19 | */ |
|
| 20 | private function __clone() |
|
| 21 | { |
|
| 22 | } |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @return self |
|
| 26 | */ |
|
| 27 | final public static function getInstance() |
|
| 28 | { |
|
| 29 | if (!self::$instance) { |
|
| 30 | $class = get_called_class(); |
|
| 31 | self::$instance = new $class; |
|
| 32 | } |
|
| 33 | return self::$instance; |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @param string $driver |
|
| 38 | * @param array $params |
|
| 39 | * @return FtpInterface |
|
| 40 | * @throws Exception |
|
| 41 | */ |
|
| 42 | final public function create($driver = self::DRIVER_FTP, $params = array()) |
|
| 43 | { |
|
| 44 | $class = $this->getClassNameForDriver($driver); |
|
| 45 | if (!class_exists($class)) { |
|
| 46 | throw new Exception('Unable to create ' . $class); |
|
| 47 | } |
|
| 48 | $instance = new $class($params); |
|
| 49 | if (!$instance instanceof FtpInterface) { |
|
| 50 | throw new Exception('Unable to create ' . $class); |
|
| 51 | } |
|
| 52 | return $instance; |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Get full class name to create |
|
| 57 | * @param string $driver |
|
| 58 | * @return string |
|
| 59 | */ |
|
| 60 | protected function getClassNameForDriver($driver) |
|
| 61 | { |
|
| 62 | return __NAMESPACE__ . '\Driver\\' . $driver; |
|
| 63 | } |
|
| 64 | } |
|
| 65 | ||