* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShopBundle\Kernel; use Doctrine\DBAL\DriverManager; /** * Class ModuleRepositoryFactory is used to build the ModuleRepository in context where symfony container is not * available or not yet initialised (ex: AppKernel, PrestaShop\PrestaShop\Adapter\ContainerBuilder). * This factory is able to fetch the necessary parameters itself and builds the database connection for ModuleRepository. * * WARNING: this factory is only to be used in the specific cases mentioned above, for any other case please use the * 'prestashop.module_kernel.repository' or 'prestashop.bundle.repository.module' depending on your needs. * * @deprecated Since 1.7.8 */ class ModuleRepositoryFactory { /** * @var self|null */ private static $instance; /** * @var array|null */ private $parameters; /** * @var ModuleRepository */ private $moduleRepository; /** * @var string */ private $parametersFile = ''; public static function getInstance() { if (!self::$instance) { self::$instance = new ModuleRepositoryFactory(); } return self::$instance; } /** * @param array|null $parameters */ public function __construct(array $parameters = null) { $this->parameters = $parameters; } /** * @return ModuleRepository * * @throws \Doctrine\DBAL\DBALException */ public function getRepository() { $parameters = $this->getParameters(); if (null !== $parameters && null === $this->moduleRepository) { $databasePrefix = $parameters['database_prefix']; $this->moduleRepository = new ModuleRepository( $this->getConnection($parameters), $databasePrefix ); } return $this->moduleRepository; } /** * @param array $parameters * * @return \Doctrine\DBAL\Connection * * @throws \Doctrine\DBAL\DBALException */ private function getConnection(array $parameters) { return DriverManager::getConnection([ 'dbname' => $parameters['database_name'], 'user' => $parameters['database_user'], 'password' => $parameters['database_password'], 'host' => $parameters['database_host'], 'port' => $parameters['database_port'], 'charset' => 'utf8', 'driver' => 'pdo_mysql', ]); } /** * @return string file path to PrestaShop configuration parameters */ private function getParametersFile() { if (empty($this->parametersFile) && file_exists(__DIR__ . '/../../../app/config/parameters.php')) { $this->parametersFile = realpath(__DIR__ . '/../../../app/config/parameters.php'); } return $this->parametersFile; } /** * @return array */ private function getParameters() { if (null === $this->parameters && !empty($this->getParametersFile())) { $config = require $this->getParametersFile(); $this->parameters = $config['parameters']; } array_walk($this->parameters, function (&$param) { $param = str_replace('%%', '%', $param); }); return $this->parameters; } }