Skip to content

Commit 162e090

Browse files
author
Fredrick Peter
committed
On Server Realpath fix
1 parent 87ccb31 commit 162e090

File tree

6 files changed

+135
-84
lines changed

6 files changed

+135
-84
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
104104
**Step 1** — update your `composer.json`:
105105
```composer.json
106106
"require": {
107-
"peterson/php-orm-database": "^2.2.9"
107+
"peterson/php-orm-database": "^3.0.1"
108108
}
109109
```
110110

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"extra": {
3939
"branch-alias": {
40-
"dev-main": "2.2.9-dev"
40+
"dev-main": "3.0.1-dev"
4141
}
4242
},
4343
"minimum-stability": "stable",

src/AutoloadEnv.php

Lines changed: 112 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -188,75 +188,134 @@ static public function configurePagination(?array $options = [])
188188
*/
189189
static protected function createDummy()
190190
{
191-
$realPath = DOT_ENV_CONNECTION['self_path']['path'];
192-
$paths = [
193-
'init' => [
194-
'path' => "{$realPath}\init.php",
195-
'dummy' => realpath(__DIR__) . "\Dummy\dummyInit.php",
196-
],
197-
'gitignore' => [
198-
'path' => "{$realPath}\.gitignore",
199-
'dummy' => realpath(__DIR__) . "\Dummy\dummyGitIgnore.php",
200-
],
201-
'htaccess' => [
202-
'path' => "{$realPath}\.htaccess",
203-
'dummy' => realpath(__DIR__) . "\Dummy\dummyHtaccess.php",
204-
],
205-
'phpini' => [
206-
'path' => "{$realPath}\php.ini",
207-
'dummy' => realpath(__DIR__) . "\Dummy\dummyPhpIni.php",
208-
],
209-
'userini' => [
210-
'path' => "{$realPath}\.user.ini",
211-
'dummy' => realpath(__DIR__) . "\Dummy\dummyUserIni.php",
212-
],
213-
];
191+
$paths = self::getPathsData();
214192

215-
// create for init
216-
if(!file_exists($paths['init']['path']) && !is_dir($paths['init']['path'])){
217-
@$fsource = fopen($paths['init']['path'], 'w+s');
218-
if(is_resource($fsource)){
219-
@fwrite($fsource, file_get_contents($paths['init']['dummy']));
220-
@fclose($fsource);
193+
// only create when files are not present
194+
if(self::isDummyNotPresent()){
195+
// create for init
196+
if(!file_exists($paths['init']['path']) && !is_dir($paths['init']['path'])){
197+
// Read the contents of the dummy file
198+
$dummyContent = file_get_contents($paths['init']['dummy']);
199+
200+
// Write the contents to the new file
201+
file_put_contents($paths['init']['path'], $dummyContent);
202+
}
203+
204+
// create for gitignore
205+
if(!file_exists($paths['gitignore']['path']) && !is_dir($paths['gitignore']['path'])){
206+
// Read the contents of the dummy file
207+
$dummyContent = file_get_contents($paths['gitignore']['dummy']);
208+
209+
// Write the contents to the new file
210+
file_put_contents($paths['gitignore']['path'], $dummyContent);
211+
}
212+
213+
// create for htaccess
214+
if(!file_exists($paths['htaccess']['path']) && !is_dir($paths['htaccess']['path'])){
215+
// Read the contents of the dummy file
216+
$dummyContent = file_get_contents($paths['htaccess']['dummy']);
217+
218+
// Write the contents to the new file
219+
file_put_contents($paths['htaccess']['path'], $dummyContent);
220+
}
221+
222+
// create for phpini
223+
if(!file_exists($paths['phpini']['path']) && !is_dir($paths['phpini']['path'])){
224+
// Read the contents of the dummy file
225+
$dummyContent = file_get_contents($paths['phpini']['dummy']);
226+
227+
// Write the contents to the new file
228+
file_put_contents($paths['phpini']['path'], $dummyContent);
229+
}
230+
231+
// create for userini
232+
if(!file_exists($paths['userini']['path']) && !is_dir($paths['userini']['path'])){
233+
// Read the contents of the dummy file
234+
$dummyContent = file_get_contents($paths['userini']['dummy']);
235+
236+
// Write the contents to the new file
237+
file_put_contents($paths['userini']['path'], $dummyContent);
221238
}
222239
}
240+
}
241+
242+
/**
243+
* Check if dummy data is present
244+
*
245+
* @return bool
246+
*/
247+
static private function isDummyNotPresent()
248+
{
249+
$paths = self::getPathsData();
250+
$present = [false];
251+
252+
// create for init
253+
if(file_exists($paths['init']['path']) && !is_dir($paths['init']['path'])){
254+
$present[] = true;
255+
}
223256

224257
// create for gitignore
225-
if(!file_exists($paths['gitignore']['path']) && !is_dir($paths['gitignore']['path'])){
226-
@$fsource = fopen($paths['gitignore']['path'], 'w+s');
227-
if(is_resource($fsource)){
228-
@fwrite($fsource, file_get_contents($paths['gitignore']['dummy']));
229-
@fclose($fsource);
230-
}
258+
if(file_exists($paths['gitignore']['path']) && !is_dir($paths['gitignore']['path'])){
259+
$present[] = true;
231260
}
232261

233262
// create for htaccess
234-
if(!file_exists($paths['htaccess']['path']) && !is_dir($paths['htaccess']['path'])){
235-
@$fsource = fopen($paths['htaccess']['path'], 'w+s');
236-
if(is_resource($fsource)){
237-
@fwrite($fsource, file_get_contents($paths['htaccess']['dummy']));
238-
@fclose($fsource);
239-
}
263+
if(file_exists($paths['htaccess']['path']) && !is_dir($paths['htaccess']['path'])){
264+
$present[] = true;
240265
}
241266

242267
// create for phpini
243-
if(!file_exists($paths['phpini']['path']) && !is_dir($paths['phpini']['path'])){
244-
@$fsource = fopen($paths['phpini']['path'], 'w+s');
245-
if(is_resource($fsource)){
246-
@fwrite($fsource, file_get_contents($paths['phpini']['dummy']));
247-
@fclose($fsource);
248-
}
268+
if(file_exists($paths['phpini']['path']) && !is_dir($paths['phpini']['path'])){
269+
$present[] = true;
249270
}
250271

251272
// create for userini
252-
if(!file_exists($paths['userini']['path']) && !is_dir($paths['userini']['path'])){
253-
@$fsource = fopen($paths['userini']['path'], 'w+s');
254-
if(is_resource($fsource)){
255-
@fwrite($fsource, file_get_contents($paths['userini']['dummy']));
256-
@fclose($fsource);
257-
}
273+
if(file_exists($paths['userini']['path']) && !is_dir($paths['userini']['path'])){
274+
$present[] = true;
258275
}
259276

277+
// Check if all elements in $present are false
278+
$allFalse = empty(array_filter($present));
279+
280+
// All elements in $present are false
281+
if ($allFalse) {
282+
return false;
283+
}
284+
285+
return true;
286+
}
287+
288+
/**
289+
* Get all dummy contents path data
290+
*
291+
* @return array
292+
*/
293+
static private function getPathsData()
294+
{
295+
$serverPath = DOT_ENV_CONNECTION['self_path']['path'];
296+
$realPath = str_replace('\\', '/', rtrim(realpath(__DIR__), "/\\"));
297+
return [
298+
'init' => [
299+
'path' => "{$serverPath}init.php",
300+
'dummy' => "{$realPath}/Dummy/dummyInit.php",
301+
],
302+
'gitignore' => [
303+
'path' => "{$serverPath}\.gitignore",
304+
'dummy' => "{$realPath}/Dummy/dummyGitIgnore.php",
305+
],
306+
'htaccess' => [
307+
'path' => "{$serverPath}\.htaccess",
308+
'dummy' => "{$realPath}/Dummy/dummyHtaccess.php",
309+
],
310+
'phpini' => [
311+
'path' => "{$serverPath}\php.ini",
312+
'dummy' => "{$realPath}/Dummy/dummyPhpIni.php",
313+
],
314+
'userini' => [
315+
'path' => "{$serverPath}\.user.ini",
316+
'dummy' => "{$realPath}/Dummy/dummyUserIni.php",
317+
],
318+
];
260319
}
261320

262321
}

src/Capsule/AppManager.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
class AppManager extends Constants{
1212

13-
1413
static private $year;
15-
1614

1715
static function initAppManager()
1816
{
@@ -68,10 +66,9 @@ static public function envDummy()
6866
PUSHER_SCHEME=https
6967
PUSHER_APP_CLUSTER=mt1
7068
71-
#ORM Model Builder
72-
#©Copyright - '. self::$year .'
73-
APP_DEVELOPER="Fredrick Peterson"
74-
APP_DEVELOPER_EMAIL="[email protected]"
69+
#©Copyright '. self::$year .'
70+
APP_DEVELOPER=
71+
APP_DEVELOPER_EMAIL=
7572
');
7673
}
7774

src/Migrations/Migration.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@ static private function initBaseDirectory()
5757

5858
// create file if not exist
5959
if (!file_exists($gitignore) && !is_dir($gitignore)) {
60-
@$fsource = fopen($gitignore, 'w+');
61-
if(is_resource($fsource)){
62-
fwrite($fsource, preg_replace(
63-
'/^[ \t]+|[ \t]+$/m', '',
64-
".
65-
/database
66-
.env"
67-
));
68-
fclose($fsource);
69-
}
60+
// Write the contents to the new file
61+
file_put_contents($gitignore, preg_replace(
62+
'/^[ \t]+|[ \t]+$/m', '',
63+
".
64+
/database
65+
.env"
66+
));
7067
}
7168
}
7269

@@ -142,17 +139,20 @@ static public function create(?string $table_name, ?string $type = null)
142139
// Date convert
143140
$fileName = sprintf( "%s_%s_%s", date('Y_m_d'), strtotime('today'), "{$case_table}.php" );
144141

142+
// real path
143+
$realPath = str_replace('\\', '/', rtrim(realpath(__DIR__), "/\\"));
144+
145145
// get directory
146-
$dummyPath = realpath(__DIR__) . "\..\Dummy\dummyMigration.php";
146+
$dummyPath = "{$realPath}/../Dummy/dummyMigration.php";
147147

148148

149149
// If type creation passed
150150
if(!empty($type) && in_array(strtolower($type), ['job', 'jobs'])){
151151
// create a jobs table
152-
$dummyPath = realpath(__DIR__) . "\..\Dummy\dummyJobsMigration.php";
152+
$dummyPath = "{$realPath}/../Dummy/dummyJobsMigration.php";
153153
} elseif(!empty($type) && in_array(strtolower($type), ['session', 'sessions'])){
154154
// create a sessions table
155-
$dummyPath = realpath(__DIR__) . "\..\Dummy\dummySessionsMigration.php";
155+
$dummyPath = "{$realPath}/../Dummy/dummySessionsMigration.php";
156156
}
157157

158158
// dummy content
@@ -173,11 +173,8 @@ static public function create(?string $table_name, ?string $type = null)
173173
}
174174

175175
// start writting
176-
@$fsource = fopen($absoluteFile, 'w+s');
177-
if(is_resource($fsource)){
178-
@fwrite($fsource, $dummyContent );
179-
@fclose($fsource);
180-
}
176+
// Write the contents to the new file
177+
file_put_contents($absoluteFile, $dummyContent);
181178

182179
echo sprintf("Table `%s` has been created
183180
<span style='background: #027b02; {$style}'>

src/Schema/OrmDotEnv.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ static public function createOrIgnore()
124124

125125
// if file doesn't exist and not a directory
126126
if(!file_exists($path) && !is_dir($path)){
127-
@$fsource = fopen($path, 'w+');
128-
if(is_resource($fsource)){
129-
@fwrite($fsource, self::envTxt());
130-
@fclose($fsource);
131-
}
127+
128+
// Write the contents to the new file
129+
file_put_contents($path, self::envTxt());
132130
}
133131
}
134132

0 commit comments

Comments
 (0)