Completed
Push — master ( 3f0046...b1c777 )
by Gino
06:41 queued 03:19
created

TDMCreateFile::getRepositoryPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: TDMCreateFile.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
xoops_load('XoopsFile');
25
/**
26
 * Class TDMCreateFile.
27
 */
28
class TDMCreateFile extends TDMCreateTableFields
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    * @var string
32
    */
33
    private $xoopsFile = null;
34
35
    /**
36
     * "fileName" attribute of the files.
37
     *
38
     * @var mixed
39
     */
40
    private $fileName = null;
41
42
    /**
43
     * "subdir" attribute of the directories.
44
     *
45
     * @var string
46
     */
47
    private $subdir = null;
48
49
    /**
50
     * "uploadPath" attribute of the files.
51
     *
52
     * @var string
53
     */
54
    private $uploadPath = null;
55
56
    /*
57
    * @var string
58
    */
59
    private $content = null;
60
61
    /*
62
    * @var mixed
63
    */
64
    private $created = null;
65
66
    /*
67
    * @var mixed
68
    */
69
    private $notCreated = null;
70
71
    /*
72
    * @var string
73
    */
74
    private $mode = null;
75
76
    /*
77
    * @var string
78
    */
79
    protected $tdmcreate = null;
80
81
    /*
82
    *  @public function constructor
83
    *  @param null
84
    */
85
    /**
86
     *
87
     */
88
    public function __construct()
89
    {
90
        parent::__construct();
91
        $this->xoopsFile = XoopsFile::getHandler();
92
        $this->tdmcreate = TDMCreateHelper::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateHelper::getInstance() of type object<TDMCreateHelper> is incompatible with the declared type string of property $tdmcreate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
93
    }
94
95
    /*
96
    *  @public static function &getInstance
97
    *  @param null
98
    */
99
    /**
100
     * @return TDMCreateFile
101
     */
102
    public static function &getInstance()
103
    {
104
        static $instance = false;
105
        if (!$instance) {
106
            $instance = new self();
107
        }
108
109
        return $instance;
110
    }
111
112
    /**
113
     * @public function create
114
     *
115
     * @param $moduleDirname
116
     * @param $subdir
117
     * @param $fileName
118
     * @param $content
119
     * @param $created
120
     * @param $notCreated
121
     * @param $mode
122
     */
123
    public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+')
124
    {
125
        $this->setFileName($fileName);
126
        $this->created = $created;
127
        $this->notCreated = $notCreated;
128
        $this->setMode($mode);
129
        $this->setRepositoryPath($moduleDirname);
130
        if (!empty($content) && is_string($content)) {
131
            $this->setContent($content);
132
        }
133
        if (isset($subdir) && is_string($subdir)) {
134
            $this->setSubDir($subdir);
135
        }
136
    }
137
138
    /*
139
    *  @public function write
140
    *  @param string $module
141
    *  @param string $fileName
142
    */
143
    /**
144
     * @param $module
145
     * @param $fileName
146
     */
147
    public function write($module, $fileName)
148
    {
149
        $this->setModule($module);
150
        $this->setFileName($fileName);
151
    }
152
153
    /*
154
    *  @private function setRepositoryPath
155
    *  @param string $moduleDirname
156
    */
157
    /**
158
     * @param $moduleDirname
159
     */
160
    private function setRepositoryPath($moduleDirname)
161
    {
162
        $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH.'/'.$moduleDirname;
163
    }
164
165
    /*
166
    *  @private function getRepositoryPath
167
    *  @param null
168
    */
169
    /**
170
     * @return string
171
     */
172
    private function getRepositoryPath()
173
    {
174
        return $this->uploadPath;
175
    }
176
177
    /*
178
    *  @private function setSubDir
179
    *  @param string $subdir
180
    */
181
    /**
182
     * @param $subdir
183
     */
184
    private function setSubDir($subdir)
185
    {
186
        $this->subdir = $subdir;
187
    }
188
189
    /*
190
    *  @private function getSubDir
191
    *  @param null
192
    */
193
    private function getSubDir()
194
    {
195
        return $this->subdir;
196
    }
197
198
    /**
199
     * public function setFileName.
200
     *
201
     * @param $fileName
202
     *
203
     * @internal param mixed $file_name
204
     */
205
    public function setFileName($fileName)
206
    {
207
        $this->fileName = $fileName;
208
    }
209
210
    /*
211
    *  @public function getFileName
212
    *  @param null
213
    */
214
    /**
215
     * @return mixed
216
     */
217
    public function getFileName()
218
    {
219
        return $this->fileName;
220
    }
221
222
    /*
223
    *  @private function setContent
224
    *  @param string $content
225
    */
226
    /**
227
     * @param $content
228
     */
229
    private function setContent($content)
230
    {
231
        $this->content = $content;
232
    }
233
234
    /*
235
    *  @private function setContent
236
    *  @param null
237
    */
238
    /**
239
     */
240
    private function getContent()
241
    {
242
        return $this->content;
243
    }
244
245
    /*
246
    *  @private function getFolderName
247
    *  @param null
248
    */
249
    /**
250
     * @return string
251
     */
252
    private function getFolderName()
253
    {
254
        $path = $this->getUploadPath();
255
        if (strrpos($path, '\\')) {
256
            $str = strrpos($path, '\\');
257
            if ($str !== false) {
258
                return substr($path, $str + 1, strlen($path));
259
            } else {
260
                return substr($path, $str, strlen($path));
261
            }
262
        }
263
        //return 'root module';
264
        return;
265
    }
266
267
    /*
268
    *  @public function getUploadPath
269
    *  @param null
270
    */
271
    /**
272
     * @return string
273
     */
274
    private function getUploadPath()
275
    {
276
        if ($this->getSubDir() != null) {
277
            $ret = $this->getRepositoryPath().'/'.$this->getSubDir();
278
        } else {
279
            $ret = $this->getRepositoryPath();
280
        }
281
282
        return $ret;
283
    }
284
285
    /*
286
    *  @private function getCreated
287
    *  @param null
288
    */
289
    /**
290
     * @return bool
291
     */
292
    private function getCreated()
293
    {
294
        return $this->created;
295
    }
296
297
    /*
298
    *  @private function getNotCreated
299
    *  @param null
300
    */
301
    /**
302
     * @return bool
303
     */
304
    private function getNotCreated()
305
    {
306
        return $this->notCreated;
307
    }
308
309
    /*
310
    *  @private function setMode
311
    *  @param string $mode
312
    */
313
    /**
314
     * @param $mode
315
     */
316
    private function setMode($mode)
317
    {
318
        $this->mode = $mode;
319
    }
320
321
    /*
322
    *  @private function getMode
323
    *  @param null
324
    */
325
    /**
326
     */
327
    private function getMode()
328
    {
329
        return $this->mode;
330
    }
331
332
    /*
333
    *  @public function getLanguage
334
    *  @param string $moduleDirname
335
    *  @param string $prefix
336
    *  @param string $suffix
337
    */
338
    /**
339
     * @param        $moduleDirname
340
     * @param string $prefix
341
     * @param string $suffix
342
     *
343
     * @return string
344
     */
345
    public function getLanguage($moduleDirname, $prefix = '', $suffix = '')
346
    {
347
        $lang = '_'.$prefix.'_'.strtoupper($moduleDirname);
348
        $ret = $lang;
349
        if (!empty($suffix) || $suffix != '_') {
350
            $ret = $lang.'_'.$suffix;
351
        } elseif ($suffix == '_') {
352
            $ret = $lang.'_';
353
        }
354
355
        return $ret;
356
    }
357
358
    /*
359
    *  @public function getLeftString
360
    *  @param string $string
361
    */
362
    /**
363
     * @param $string
364
     *
365
     * @return string
366
     */
367
    public function getLeftString($string)
368
    {
369
        return substr($string, 0, strpos($string, '_'));
370
    }
371
372
    /*
373
    *  @public function getRightString
374
    *  @param string $string
375
    */
376
    /**
377
     * @param $string
378
     *
379
     * @return string
380
     */
381
    public function getRightString($string)
382
    {
383
        if (strpos($string, '_')) {
384
            $str = strpos($string, '_');
385
            if ($str !== false) {
386
                $ret = substr($string, $str + 1, strlen($string));
387
388
                return $ret;
389
            }
390
        }
391
392
        return $string;
393
    }
394
395
    /*
396
     *  @public function getCamelCase
397
     *  @param $string
398
     */
399
    /**
400
     * @param $string
401
     * @param $ucfirst
402
     * @param $lcfirst
403
     *
404
     * @return string
405
     */
406
    public function getCamelCase($string, $ucfirst = false, $lcfirst = false)
407
    {
408
        $rightString = $this->getRightString($string);
409
        $leftString = $this->getLeftString($string);
410
        if ($ucfirst) {
411
            $ret = $this->getUcfirst($leftString).$this->getUcfirst($rightString);
412
413
            return $ret;
414
        }
415
        if ($lcfirst) {
416
            $ret = $this->getlcfirst($leftString).$this->getUcfirst($rightString);
417
418
            return $ret;
419
        }
420
421
        return $string;
422
    }
423
424
    /*
425
     *  @public function getUcfirst
426
     *  @param $string
427
     */
428
    /**
429
     * @param $string
430
     *
431
     * @return string
432
     */
433
    public function getUcfirst($string)
434
    {
435
        return ucfirst($string);
436
    }
437
438
    /*
439
     *  @public function getLcfirst
440
     *  @param $string
441
     */
442
    /**
443
     * @param $string
444
     *
445
     * @return string
446
     */
447
    public function getLcfirst($string)
448
    {
449
        return lcfirst($string);
450
    }
451
452
    /*
453
     *  @public function getStrToUpper
454
     *  @param $string
455
     */
456
    /**
457
     * @param $string
458
     *
459
     * @return string
460
     */
461
    public function getStrToUpper($string)
462
    {
463
        return strtoupper($string);
464
    }
465
466
    /*
467
     *  @public function getStrToLower
468
     *  @param $string
469
     */
470
    /**
471
     * @param $string
472
     *
473
     * @return string
474
     */
475
    public function getStrToLower($string)
476
    {
477
        return strtolower($string);
478
    }
479
480
    /*
481
    *  @public function getInclude
482
    *  @param $filename
483
    */
484
    /**
485
     * @return string
486
     */
487
    public function getInclude($filename = 'header')
488
    {
489
        return "include __DIR__ . '/{$filename}.php';\n";
490
    }
491
492
    /*
493
    *  @public function getIncludeOnce
494
    *  @param $filename
495
    */
496
    /**
497
     * @return string
498
     */
499
    public function getIncludeOnce($filename = 'header')
500
    {
501
        return "include_once __DIR__ . '/{$filename}.php';\n";
502
    }
503
504
    /*
505
     * @public function getCommentLine
506
     * @param $string     
507
     * @param $var
508
     *
509
     * @return string
510
     */
511
    public function getCommentLine($string = '', $var = '')
512
    {
513
        $value = !empty($var) ? ' '.$var : '';
514
        $ret = "// {$string}{$value}\n";
515
516
        return $ret;
517
    }
518
519
    /**
520
     * @private function getHeaderComment
521
     *
522
     * @param $comment
523
     *
524
     * @return string
525
     */
526
    public function getHeaderComment($comment)
527
    {
528
        return "// ------------------- {$comment} ------------------- //\n";
529
    }
530
531
    /*
532
     * @public function getSimpleString
533
     * @param $string     
534
     *
535
     * @return string
536
     */
537
    public function getSimpleString($string)
538
    {
539
        return "{$string}\n";
540
    }
541
542
    /*
543
    *  @public function getHeaderFilesComments
544
    *  @param string $module
545
    *  @param string $fileName
546
    */
547
    /**
548
     * @param $module
549
     * @param $fileName
550
     * @param $noPhpFile
551
     *
552
     * @return string
553
     */
554
    public function getHeaderFilesComments($module, $fileName, $noPhpFile = null)
555
    {
556
        $name = $module->getVar('mod_name');
557
        $dirname = $module->getVar('mod_dirname');
558
        $version = $module->getVar('mod_version');
559
        $since = $module->getVar('mod_since');
560
        $minXoops = $module->getVar('mod_min_xoops');
561
        $author = $module->getVar('mod_author');
562
        $credits = $module->getVar('mod_credits');
563
        $authorMail = $module->getVar('mod_author_mail');
564
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
565
        $license = $module->getVar('mod_license');
566
        $subversion = $module->getVar('mod_subversion');
567
        $date = date('D Y-m-d H:i:s');
568
        if (is_null($noPhpFile)) {
569
            $ret = <<<EOT
570
<?php
571
/*\n
572
EOT;
573
        } elseif (is_string($noPhpFile)) {
574
            $ret = <<<EOT
575
{$noPhpFile}
576
/*\n
577
EOT;
578
        } else {
579
            $ret = <<<EOT
580
/*\n
581
EOT;
582
        }
583
        // $ret .= file_get_contents('./docs/top_license_file.txt');
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
584
        $ret .= <<<EOT
585
 You may not change or alter any portion of this comment or credits
586
 of supporting developers from this source code or any supporting source code
587
 which is considered copyrighted (c) material of the original comment or credit authors.
588
589
 This program is distributed in the hope that it will be useful,
590
 but WITHOUT ANY WARRANTY; without even the implied warranty of
591
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
592
*/
593
/**
594
 * {$name} module for xoops
595
 *
596
 * @copyright       The XOOPS Project (http://xoops.org)
597
 * @license         {$license}
598
 * @package         {$dirname}
599
 * @since           {$since}
600
 * @min_xoops       {$minXoops}
601
 * @author          {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>
602
 * @version         \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$
603
 */\n
604
EOT;
605
606
        return $ret;
607
    }
608
609
    /*
610
    *  @public function renderFile
611
    *  @param null
612
    */
613
    /**
614
     * @return string
615
     */
616
    public function renderFile()
617
    {
618
        $fileName = $this->getFileName();
619
        $path = $this->getUploadPath().'/'.$fileName;
620
        $created = $this->getCreated();
621
        $notCreated = $this->getNotCreated();
622
        $folderName = $this->getFolderName();
623
        $mode = $this->getMode();
624
        $ret = '';
625
        if (!$this->xoopsFile->__construct($path, true)) {
0 ignored issues
show
Bug introduced by
The method __construct cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
626
            // Force to create file if not exist
627
            if ($this->xoopsFile->open($mode, true)) {
0 ignored issues
show
Bug introduced by
The method open cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
628
                if ($this->xoopsFile->writable()) {
0 ignored issues
show
Bug introduced by
The method writable cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
629
                    // Integration of the content in the file
630
                    if (!$this->xoopsFile->write($this->getContent(), $mode, true)) {
0 ignored issues
show
Bug introduced by
The method write cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
631
                        $ret .= sprintf($notCreated, $fileName, $folderName);
632
                        $GLOBALS['xoopsTpl']->assign('created', false);
633
                        exit();
634
                    }
635
                    // Created
636
                    $ret .= sprintf($created, $fileName, $folderName);
637
                    $GLOBALS['xoopsTpl']->assign('created', true);
638
                    $this->xoopsFile->close();
0 ignored issues
show
Bug introduced by
The method close cannot be called on $this->xoopsFile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
639
                } else {
640
                    $ret .= sprintf($notCreated, $fileName, $folderName);
641
                    $GLOBALS['xoopsTpl']->assign('created', false);
642
                }
643
            } else {
644
                $ret .= sprintf($notCreated, $fileName, $folderName);
645
                $GLOBALS['xoopsTpl']->assign('created', false);
646
            }
647
        }
648
649
        return $ret;
650
    }
651
}
652