1: <?php
2:
3: App::uses('CssBox', 'Layouts.Lib');
4:
5: class CssController extends AppController {
6:
7: public function beforeFilter() {
8: parent::beforeFilter();
9:
10: if (isset($this->Auth)) {
11: $this->Auth->allow('process');
12: }
13: }
14:
15: public function process($path) {
16: if (file_exists($this->_cssFilePath($path))) {
17: $this->_serveCss(file_get_contents($this->_cssFilePath($path)));
18: } else {
19: throw new Exception("File not found: {$this->_cssFilePath($path)}");
20: }
21: }
22:
23: public function css_box($selectors) {
24: $box = new CssBox($this->request->params['named']);
25: $this->_serveCss($box->build($selectors));
26: }
27:
28: private function _serveCss($content) {
29: $this->response->type('text/css');
30:
31:
32: foreach ($this->_variables() as $name => $value) {
33: $content = preg_replace('/\$' . preg_quote($name) . '(?!\w)/', $value, $content);
34: }
35:
36: $this->response->body($content);
37: $this->autoRender = false;
38: $this->autoLayout = false;
39: return $this->response;
40: }
41:
42: private function _variables() {
43: if (file_exists($this->_variablesFilePath())) {
44: $vars = parse_ini_file($this->_variablesFilePath());
45: } else {
46: $vars = array();
47: }
48:
49: $vars['WEBROOT'] = Router::url('/', true);
50:
51: return $vars;
52: }
53:
54: private function _variablesFilePath() {
55: return $this->_cssDirectoryPath() . DS . 'variables.ini';
56: }
57:
58: private function _cssDirectoryPath() {
59: return ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS . 'css';
60: }
61:
62: private function _cssFilePath($path) {
63: return ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS . 'css' . DS . $path . '.css';
64: }
65:
66: }
67:
68: ?>
69: