Last active
August 9, 2020 12:45
-
-
Save msacar/9f4e5c8b368025c6d0b2a85bc020d25a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Managar; | |
class Gsm | |
{ | |
protected static $instance = null; | |
public $drivers = []; | |
/** | |
* $instance field'ımıza | |
* sınıf instance edilmemişse | |
* instancelayıp değişkene kaydeder, | |
* sınıf zaten instancelı ise | |
* $instance değişkenini döndürür. | |
* | |
* @return static | |
*/ | |
public static function bootIfNotBooted() | |
{ | |
if (!self::$instance) { | |
return self::$instance = (new static); | |
} | |
} | |
/** | |
* İstenilen Driver instance edilir ve $drivers field'ımıza instance lı hali eklenir | |
* daha önce driver oluşturulmuşsa $drivers field'ından alınır. | |
* | |
* @param $name | |
* @return mixed | |
*/ | |
public function resolveDriver($name) | |
{ | |
if (isset($this->drivers[$name])) { | |
return $this->drivers[$name]; | |
} | |
$methodName = 'create' . ucfirst($name) . 'Driver'; | |
return $this->drivers[$name] = $this->$methodName(); | |
} | |
/** | |
* Manager'ı statik olarak kullanabilmemizi sağlıyor. | |
* Driver'ın resolve u için gerekiyor. | |
* Manager'a kendini instancelatıp _driver methodunu kullandırttırıyor. | |
* | |
* @param $name | |
* @return mixed | |
*/ | |
public static function driver($name = null) | |
{ | |
self::bootIfNotBooted(); | |
return self::$instance->_driver($name); | |
} | |
/** | |
* Kullanılacak driver'ı belirler | |
* Eğer driver belirtilmemişse defaultDriver seçilir. | |
* Driver instance edilmemişse resolveDriver methodunu kullanır ve oluşturup döndürür. | |
* | |
* @param null $name | |
* @return mixed | |
*/ | |
public function _driver($name = null) | |
{ | |
$name = $name ?: $this->getDefaultDriver(); | |
return $this->resolveDriver($name); | |
} | |
/** | |
* VodafoneDriverı oluşturur | |
* @return VodafoneDriver | |
*/ | |
public function createVodafoneDriver() | |
{ | |
return new VodafoneDriver(); | |
} | |
/** | |
* TurkcellDriver oluşturur | |
* @return TurkcellDriver | |
*/ | |
public function createTurkcellDriver() | |
{ | |
return new TurkcellDriver(); | |
} | |
private function getDefaultDriver() | |
{ | |
return "vodafoneDriver"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment