| 1 |
<?php namespace Premmerce\SDK\V2\FileManager; |
| 2 |
|
| 3 |
|
| 4 |
class FileManager{ |
| 5 |
|
| 6 |
/** |
| 7 |
* @var string |
| 8 |
*/ |
| 9 |
private $mainFile; |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $pluginDirectory; |
| 16 |
|
| 17 |
|
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $pluginName; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $pluginUrl; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $themeDirectory; |
| 33 |
|
| 34 |
/** |
| 35 |
* PluginManager constructor. |
| 36 |
* |
| 37 |
* @param string $mainFile |
| 38 |
* @param string|null $themeDirectory |
| 39 |
*/ |
| 40 |
public function __construct($mainFile, $themeDirectory = null){ |
| 41 |
$this->mainFile = $mainFile; |
| 42 |
$this->pluginDirectory = plugin_dir_path($this->mainFile); |
| 43 |
$this->pluginName = basename($this->pluginDirectory); |
| 44 |
$this->themeDirectory = $themeDirectory?: $this->pluginName; |
| 45 |
$this->pluginUrl = plugin_dir_url($this->getMainFile()); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function getPluginDirectory(){ |
| 53 |
return $this->pluginDirectory; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public function getPluginName(){ |
| 60 |
return $this->pluginName; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function getMainFile(){ |
| 67 |
return $this->mainFile; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function getPluginUrl(){ |
| 74 |
return $this->pluginUrl; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param string $__template |
| 79 |
* @param array $__variables |
| 80 |
*/ |
| 81 |
public function includeTemplate($__template, array $__variables = []){ |
| 82 |
if($__template = $this->locateTemplate($__template)){ |
| 83 |
extract($__variables); |
| 84 |
include $__template; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* @param string $template |
| 91 |
* @param array $variables |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function renderTemplate($template, array $variables = []){ |
| 96 |
ob_start(); |
| 97 |
$this->includeTemplate($template, $variables); |
| 98 |
$content = ob_get_contents(); |
| 99 |
ob_end_clean(); |
| 100 |
|
| 101 |
return $content; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @param string $file |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function locateAsset($file){ |
| 110 |
return $this->pluginUrl . 'assets/' . $file; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* @param string $template |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
public function locateTemplate($template){ |
| 120 |
|
| 121 |
if(strpos($template, 'frontend/') === 0){ |
| 122 |
|
| 123 |
$frontendTemplate = str_replace('frontend/', '', $template); |
| 124 |
|
| 125 |
if($file = locate_template($this->themeDirectory . '/' . $frontendTemplate)){ |
| 126 |
|
| 127 |
return $file; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
return $this->pluginDirectory . 'views/' . $template; |
| 133 |
} |
| 134 |
|
| 135 |
} |