Passed
Push — 1.0.0-dev ( 8851fa...9b3671 )
by nguereza
02:38
created

Assets::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    /**
32
     *  @file Assets.php
33
     *    
34
     *  This class contains static methods for generating static content links (images, Javascript, CSS, etc.).
35
     *  
36
     *  @package	core	
37
     *  @author	TNH Framework team
38
     *  @copyright	Copyright (c) 2017
39
     *  @license	http://opensource.org/licenses/MIT	MIT License
40
     *  @link	http://www.iacademy.cf
41
     *  @version 1.0.0
42
     *  @since 1.0.0
43
     *  @filesource
44
     */
45
    class Assets extends BaseClass {
46
47
         /**
48
         * Construct new instance
49
         */
50
        public function __construct() {
51
            parent::__construct();
52
        }
53
54
        /**
55
         *  Generate the link of the assets file.
56
         *  
57
         *  Generates the absolute link of a file inside ASSETS_PATH folder.
58
         *  For example :
59
         *  	path('foo/bar/css/style.css'); => http://mysite.com/assets/foo/bar/css/style.css
60
         *  Note:
61
         *  The argument passed to this function must be the relative link to the 
62
         *  folder that contains the static contents defined by the constant ASSETS_PATH.
63
         *  
64
         *  @param string $asset the name of the assets file path with the extension.
65
         *  @return string|null the absolute path of the assets file, if it exists o
66
         *  therwise returns null if the file does not exist.
67
         */
68
        public function path($asset) {
69
            $path = ASSETS_PATH . $asset;
70
            $this->logger->debug('Including the Assets file [' . $path . ']');
71
            //Check if the file exists
72
            if (file_exists($path)) {
73
                $this->logger->info('Assets file [' . $path . '] included successfully');
74
                return get_instance()->url->mainUrl($path);
75
            }
76
            $this->logger->warning('Assets file [' . $path . '] does not exist');
77
            return null;
78
        }
79
		
80
        /**
81
         *  Generate the link of the css file.
82
         *  
83
         *  Generates the absolute link of a file containing the CSS style.
84
         *  For example :
85
         *  	css('mystyle'); => http://mysite.com/assets/css/mystyle.css
86
         *  Note:
87
         *  The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH.
88
         *  
89
         *  @param string $path the name of the css file without the extension.
90
         *  @return string|null the absolute path of the css file, if it exists otherwise returns null if the file does not exist.
91
         */
92
        public function css($path) {
93
            /*
94
			* if the file name contains the ".css" extension, replace it with 
95
			* an empty string for better processing.
96
			*/
97
            $path = str_ireplace('.css', '', $path);
98
            $path = ASSETS_PATH . 'css/' . $path . '.css';
99
            $this->logger->debug('Including the Assets file [' . $path . '] for CSS');
100
            //Check if the file exists
101
            if (file_exists($path)) {
102
                $this->logger->info('Assets file [' . $path . '] for CSS included successfully');
103
                return get_instance()->url->mainUrl($path);
104
            }
105
            $this->logger->warning('Assets file [' . $path . '] for CSS does not exist');
106
            return null;
107
        }
108
109
        /**
110
         *  Generate the link of the javascript file.
111
         *  
112
         *  Generates the absolute link of a file containing the javascript.
113
         *  For example :
114
         *  	js('myscript'); => http://mysite.com/assets/js/myscript.js
115
         *  Note:
116
         *  The argument passed to this function must be the relative link to 
117
         *  the folder that contains the static contents defined by the constant ASSETS_PATH.
118
         *  
119
         *  @param string $path the name of the javascript file without the extension.
120
         *  @return string|null the absolute path of the javascript file, 
121
         *  if it exists otherwise returns null if the file does not exist.
122
         */
123
        public function js($path) {
124
            $path = str_ireplace('.js', '', $path);
125
            $path = ASSETS_PATH . 'js/' . $path . '.js';
126
            $this->logger->debug('Including the Assets file [' . $path . '] for javascript');
127
            if (file_exists($path)) {
128
                $this->logger->info('Assets file [' . $path . '] for Javascript included successfully');
129
                return get_instance()->url->mainUrl($path);
130
            }
131
            $this->logger->warning('Assets file [' . $path . '] for Javascript does not exist');
132
            return null;
133
        }
134
135
        /**
136
         *  Generate the link of the image file.
137
         *  
138
         *  Generates the absolute link of a file containing the image.
139
         *  For example :
140
         *  	img('myimage.png'); => http://mysite.com/assets/images/myimage.png
141
         *  Note:
142
         *  The argument passed to this function must be the relative link to 
143
         *  the folder that contains the static contents defined by the constant ASSETS_PATH.
144
         *  
145
         *  @param string $path the name of the image file with the extension.
146
         *  @return string|null the absolute path of the image file, if it exists 
147
         *  otherwise returns null if the file does not exist.
148
         */
149
        public function img($path) {
150
            $path = ASSETS_PATH . 'images/' . $path;
151
            $this->logger->debug('Including the Assets file [' . $path . '] for image');
152
            if (file_exists($path)) {
153
                $this->logger->info('Assets file [' . $path . '] for image included successfully');
154
                return get_instance()->url->mainUrl($path);
155
            }
156
            $this->logger->warning('Assets file [' . $path . '] for image does not exist');
157
            return null;
158
        }
159
    }
160