forked from thelia-modules/INSEEGeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
INSEEGeo.php
109 lines (96 loc) · 4.05 KB
/
INSEEGeo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace INSEEGeo;
use INSEEGeo\Model\Base\InseeGeoMunicipalityQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\Finder\Finder;
use Thelia\Install\Database;
use Thelia\Log\Tlog;
use Thelia\Module\BaseModule;
/**
* Class INSEEGeo
* @package INSEEGeo
*/
class INSEEGeo extends BaseModule
{
/** @var string */
const DOMAIN_NAME = 'inseegeo';
/**
* {@inheritdoc}
* @param ConnectionInterface|null $con
*/
public function postActivation(ConnectionInterface $con = null)
{
$database = new Database($con);
// Create tables and insert data it not exist
try {
InseeGeoMunicipalityQuery::create()->findOne();
} catch (PropelException $e) {
$database->insertSql(
null,
[
__DIR__ . DS . 'Config' . DS . 'thelia.sql',
__DIR__ . DS . 'Config' . DS . 'insert.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_0.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_1.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_2.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_3.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_18_0.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_18_1.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_18_2.sql',
__DIR__ . DS . 'Config' . DS . 'insert_city_18_3.sql'
]
);
}
}
const PHP_DIR = 'Setup/Update/';
/**
* Update method to execute sql script. Script must be in Config\Update
*
* @param string $currentVersion
* @param string $newVersion
* @param ConnectionInterface $con
*/
public function update($currentVersion, $newVersion, ConnectionInterface $con)
{
$logger = Tlog::getInstance();
/** @var string $moduleDir Need for include_once php file*/
$moduleDir = __DIR__;
$setupDir = __DIR__ . DS . 'Setup' . DS . 'Update';
$finder = (new Finder)
->files()
->name('#.*?\.sql#')
->in($setupDir);
$database = new Database($con);
/** @var \Symfony\Component\Finder\SplFileInfo $updateSQLFile */
foreach ($finder as $updateSQLFile) {
if (version_compare($currentVersion, str_replace('.sql', '', $updateSQLFile->getFilename()), '<')) {
$logger->info('executing file '.$currentVersion . '.sql');
$database->insertSql(
null,
[
$updateSQLFile->getPathname()
]
);
$logger->info('end executing file '.$currentVersion . '.sql');
}
}
// php update
$filename = $setupDir . DS . $newVersion . '.php';
if (file_exists($filename)) {
$logger->info('executing file '.$newVersion . '.php');
include_once($filename);
$logger->info('end executing file '.$newVersion . '.php');
}
}
}