Stencil_Implementation::__construct()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 8.8571
cc 3
eloc 13
nc 3
nop 0
1
<?php
2
/**
3
 * Base class for extending Stencil Implementations
4
 *
5
 * Author: Jip Moors ([email protected])
6
 * Date: 4 juli 2015
7
 *
8
 * @package Stencil
9
 */
10
11
/**
12
 * Class Implementation
13
 */
14
abstract class Stencil_Implementation implements Stencil_Interface, Stencil_Implementation_Interface {
15
	/**
16
	 * The selected template engine
17
	 *
18
	 * @var $engine
19
	 */
20
	protected $engine;
21
22
	/**
23
	 * Path to save caching files
24
	 *
25
	 * @var string
26
	 */
27
	protected $cache_path;
28
29
	/**
30
	 * Path to save compiled template files
31
	 *
32
	 * @var string
33
	 */
34
	protected $compile_path;
35
36
	/**
37
	 * Path to look for templates/views
38
	 *
39
	 * @var array
40
	 */
41
	protected $template_path;
42
43
	/**
44
	 * Template file extension to check is_file against
45
	 * This is needed to determine what template should be loaded
46
	 * according to the WordPress hierarchy
47
	 *
48
	 * @var string
49
	 */
50
	protected $template_extension = 'tpl';
51
52
	/**
53
	 * Sets defaults like cache, compile and template directory paths.
54
	 *
55
	 * @throws Exception When cache path cannot be used.
56
	 */
57
	public function __construct() {
58
		$upload_dir = wp_upload_dir();
59
60
		$current_theme = wp_get_theme();
61
		$theme_slug    = $current_theme->get_stylesheet();
62
63
		/**
64
		 * Make the upload folder the root for changing files, this is the place
65
		 * that is most likely writable.
66
		 *
67
		 * Keeping the theme name as container prevents accidental problems with
68
		 * caching or compiling files when switching themes.
69
		 */
70
		$root = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $theme_slug;
71
72
		$this->cache_path   = implode( DIRECTORY_SEPARATOR, array( $root, 'cache', '' ) );
73
		$this->compile_path = implode( DIRECTORY_SEPARATOR, array( $root, 'compiled', '' ) );
74
75
		/**
76
		 * Filter: views directory
77
		 */
78
		$views_path = Stencil_Environment::filter( 'path-views', 'views' );
79
80
		/**
81
		 * Get all directories (root + optional child theme)
82
		 */
83
		$this->template_path = Stencil_File_System::get_potential_directories( $views_path );
84
85
		/**
86
		 * Attempt to make the directories
87
		 */
88
		if ( ! wp_mkdir_p( $this->cache_path ) ) {
89
			throw new Exception( 'Cache path could not be created.' );
90
		}
91
92
		if ( ! wp_mkdir_p( $this->compile_path ) ) {
93
			throw new Exception( 'Compile path could not be created.' );
94
		}
95
	}
96
97
	/**
98
	 * Called when the implementation is ready
99
	 */
100
	protected function ready() {
101
		Stencil_Environment::trigger( 'engine_ready', $this );
102
	}
103
104
	/**
105
	 * Fetch the engine so interaction is possible
106
	 *
107
	 * @return mixed
108
	 */
109
	public function get_engine() {
110
		return $this->engine;
111
	}
112
113
	/**
114
	 * Get the template path
115
	 *
116
	 * @return string|array
117
	 */
118
	public function get_template_path() {
119
		return $this->template_path;
120
	}
121
122
	/**
123
	 * Get the file extension of the templates
124
	 *
125
	 * @return string
126
	 */
127
	public function get_template_extension() {
128
		return $this->template_extension;
129
	}
130
131
	/**
132
	 * Displays a chosen template
133
	 *
134
	 * Uses fetch to fetch the output
135
	 *
136
	 * @param string $template Template file to use.
137
	 */
138
	public function display( $template ) {
139
		echo $this->fetch( $template );
140
	}
141
}
142