77namespace Magefan \GeoIp \Model ;
88
99use Magento \Store \Model \ScopeInterface ;
10+ use Magento \Framework \Filesystem \DirectoryList ;
11+ use Magento \Framework \Module \Dir as ModuleDir ;
1012
1113/**
1214 * Class IpToCountryRepository
@@ -54,21 +56,37 @@ class IpToCountryRepository
5456 */
5557 protected $ request ;
5658
59+ /**
60+ * @var DirectoryList
61+ */
62+ private $ directoryList ;
63+
64+ /**
65+ * @var ModuleDir
66+ */
67+ private $ moduleDir ;
68+
5769 /**
5870 * IpToCountryRepository constructor.
5971 * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
6072 * @param ResourceModel\IpToCountry\CollectionFactory $ipToCountryCollectionFactory
73+ * @param DirectoryList $directoryList
74+ * @param ModuleDir $moduleDir
6175 * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
6276 * @param \Magento\Framework\App\RequestInterface $httpRequest
6377 */
6478 public function __construct (
6579 \Magento \Framework \HTTP \PhpEnvironment \RemoteAddress $ remoteAddress ,
6680 ResourceModel \IpToCountry \CollectionFactory $ ipToCountryCollectionFactory ,
81+ DirectoryList $ directoryList ,
82+ ModuleDir $ moduleDir ,
6783 $ config = null ,
6884 $ httpRequest = null
6985 ) {
7086 $ this ->remoteAddress = $ remoteAddress ;
7187 $ this ->ipToCountryCollectionFactory = $ ipToCountryCollectionFactory ;
88+ $ this ->directoryList = $ directoryList ;
89+ $ this ->moduleDir = $ moduleDir ;
7290
7391 $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
7492 $ this ->config = $ config ?: $ objectManager ->get (
@@ -115,7 +133,7 @@ public function getCountryCode($ip)
115133 $ params = $ rf ->getParameters ();
116134 if (!$ params || !is_array ($ params ) || count ($ params ) < 2 ) { /* Fix for custom geoip php libraries, so 0 or 1 params */
117135 try {
118- $ this ->ipToCountry [$ ip ] = geoip_country_code_by_name ($ ip );
136+ $ this ->ipToCountry [$ ip ] = geoip_country_code_by_name ($ ip );
119137 } catch (\Exception $ e ) {
120138 //do nothing
121139 }
@@ -125,10 +143,11 @@ public function getCountryCode($ip)
125143
126144 if (!$ this ->ipToCountry [$ ip ]) {
127145 try {
128- if (file_exists (realpath (dirname (__FILE__ ) . '/../../../../../var/magefan/geoip/GeoLite2-Country.mmdb ' ))) {
129- $ datFile = realpath (dirname (__FILE__ ) . '/../../../../../var/magefan/geoip/GeoLite2-Country.mmdb ' );
146+ $ filename = $ this ->directoryList ->getPath ('var ' ) . DIRECTORY_SEPARATOR . 'magefan/geoip/GeoLite2-Country.mmdb ' ;
147+ if (file_exists ($ filename )) {
148+ $ datFile = $ filename ;
130149 } else {
131- $ datFile = realpath ( dirname ( __FILE__ ) . '/../ data/GeoLite2-Country.mmdb ' ) ;
150+ $ datFile = $ this -> moduleDir -> getDir ( ' Magefan_GeoIp ' ) . '/data/GeoLite2-Country.mmdb ' ;
132151 }
133152 $ reader = new \GeoIp2 \Database \Reader ($ datFile );
134153 $ record = $ reader ->country ($ ip );
0 commit comments