Completed
Branch master (3ee3d5)
by Pierre-Henry
35:14
created

SysVar::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.4285
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 23 and the first side effect is on line 15.

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
 * @title            SysVar Class
4
 * @desc             Parse the global pH7CMS variables.
5
 *
6
 * @author           Pierre-Henry Soria <[email protected]>
7
 * @copyright        (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
8
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
9
 * @package          PH7 / Framework / Parse
10
 * @version          1.7
11
 */
12
13
namespace PH7\Framework\Parse;
14
15
defined('PH7') or exit('Restricted access');
16
17
use PH7\Framework\Registry\Registry;
18
use PH7\Framework\Core\Kernel;
19
use PH7\Framework\Ip\Ip;
20
use PH7\Framework\Mvc\Router\Uri;
21
use PH7\Framework\Session\Session;
22
23
class SysVar
24
{
25
    const REGEX_NOT_PARSING = '/#!.+!#/';
26
27
    /** @var string */
28
    private $sVar;
29
30
    /**
31
     * Parser for the System variables.
32
     *
33
     * @param string $sVar
34
     *
35
     * @return The new parsed text
36
     */
37
    public function parse($sVar)
38
    {
39
        $this->sVar = $sVar;
40
41
        if ($this->notParsingVars()) {
42
            $this->removeNotParsingDelimiters();
43
            return $this->sVar;
44
        }
45
46
        $this->parseSiteVars();
47
        $this->parseAffiliateVars();
48
        $this->parseGlobalVars();
49
        $this->parseKernelVars();
50
51
        // Output
52
        return $this->sVar;
53
    }
54
55
    private function parseSiteVars()
56
    {
57
        $oRegistry = Registry::getInstance();
58
        $this->sVar = str_replace('%site_name%', $oRegistry->site_name, $this->sVar);
59
        $this->sVar = str_replace('%url_relative%', PH7_RELATIVE, $this->sVar);
60
        $this->sVar = str_replace(array('%site_url%','%url_root%'), $oRegistry->site_url, $this->sVar);
61
        $this->sVar = str_replace('%url_static%', PH7_URL_STATIC , $this->sVar);
62
        unset($oRegistry);
63
    }
64
65
    private function parseAffiliateVars()
66
    {
67
        $oSession = new Session;
68
        $sAffUsername = ($oSession->exists('affiliate_username')) ? $oSession->get('affiliate_username') : 'aid';
69
        $this->sVar = str_replace('%affiliate_url%', Uri::get('affiliate','router','refer', $sAffUsername), $this->sVar);
70
        unset($oSession);
71
    }
72
73
    private function parseGlobalVars()
74
    {
75
        $this->sVar = str_replace('%ip%', Ip::get(), $this->sVar);
76
    }
77
78
    private function parseKernelVars()
79
    {
80
        $this->sVar = str_replace('%software_name%', Kernel::SOFTWARE_NAME, $this->sVar);
81
        $this->sVar = str_replace('%software_author%', 'Pierre-Henry Soria', $this->sVar);
82
        $this->sVar = str_replace('%software_version_name%', Kernel::SOFTWARE_VERSION_NAME, $this->sVar);
83
        $this->sVar = str_replace('%software_version%', Kernel::SOFTWARE_VERSION, $this->sVar);
84
        $this->sVar = str_replace('%software_build%', Kernel::SOFTWARE_BUILD, $this->sVar);
85
        $this->sVar = str_replace('%software_email%', Kernel::SOFTWARE_EMAIL, $this->sVar);
86
        $this->sVar = str_replace('%software_website%', Kernel::SOFTWARE_WEBSITE, $this->sVar);
87
    }
88
89
    private function removeNotParsingDelimiters()
90
    {
91
        $this->sVar = str_replace(array('#!', '!#'), '', $this->sVar);
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    private function notParsingVars()
98
    {
99
        return preg_match(self::REGEX_NOT_PARSING, $this->sVar);
100
    }
101
}
102