Subversion Repositories PHPX

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37 PointedEar 1
<?php
2
 
3
namespace de\pointedears\css\least;
4
 
5
require_once 'Base.php';
6
 
7
require_once 'Parser.php';
8
 
9
define('DEBUG', 2);
10
 
11
/**
12
 * @author Thomas 'PointedEars' Lahn
13
 */
14
class LEAST
15
{
16
  /**
17
   * Default name of parsed file
18
   *
19
   * @var string
20
   */
21
  protected $_filename;
22
 
23
  /**
24
   * Default name of parsed file
25
   *
26
   * @param string $filename
27
   */
28
  public function __construct ($code = null)
29
  {
30
    if ($code !== null)
31
    {
32
       $this->_code = $code;
33
    }
34
  }
35
 
36
  /**
37
   * Gets a template variable or a property
38
   *
39
   * @param string $name
40
   * @throws InvalidArgumentException
41
   * @return mixed
42
   */
43
  public function __get($name)
44
  {
45
    if (property_exists($this, "_$name"))
46
    {
47
      return $this->{"_$name"};
48
    }
49
 
50
    if (array_key_exists($name, $this->_vars))
51
    {
52
      return $this->_vars[$name];
53
    }
54
 
55
    throw new InvalidArgumentException("no property '{$name}'");
56
  }
57
 
58
  /**
59
   * Sets a template variable
60
   *
61
   * @param string $name
62
   * @param mixed $value
63
   */
64
  public function __set($name, $value)
65
  {
66
    $this->_vars[$name] = $value;
67
  }
68
 
69
  /**
70
   * Replaces characters in LEAST source code
71
   *
72
   * @param string $count
73
   *   Number of characters to replace
74
   * @param string $replacement
75
   *   Replacement string
76
   * @param string $haystack
77
   *   String to be searched
78
   * @param int $start
79
   *   Start replace from here
80
   * @return string
81
   */
82
  protected static function replace ($count, $replacement, $haystack, $start)
83
  {
84
    return substr($haystack, 0, $start) . $replacement . substr($haystack, $start + $count);
85
  }
86
 
87
  /**
88
   * Parses LEAST source code, replacing special tokens, and returns the result
89
   *
90
   * @param string[optional] $code
91
   * @return string
92
   */
93
  public function parse ($code = null)
94
  {
95
    if ($code === null)
96
    {
97
      $code = $this->_code;
98
    }
99
 
100
    static $nl = 'n';
101
 
102
    \mb_internal_encoding(mb_detect_encoding($code));
103
    $parser = new \de\pointedears\css\least\Parser($code);
104
    $parser->parse();
105
 
106
    return $parser->compiled;
107
//     return $code;
108
  }
109
 
110
  /**
111
   * Parses a LEAST file and returns the result
112
   *
113
   * @param string $filename
114
   * @return string
115
   */
116
  public function parse_file ($filename = null)
117
  {
118
    $contents = file_get_contents(
119
      $filename === null ? $this->filename : $filename);
120
    return $this->parse($contents);
121
  }
122
 
123
  /**
124
   * Compiles a CSS file from a LEAST file
125
   *
126
   * @param string $source
127
   * @param string $target
128
   */
129
  public static function compile ($source, $target)
130
  {
131
    if (@filemtime($target) < @filemtime($source))
132
    {
133
      $least = new self();
134
      file_put_contents($target, $least->parse_file($source));
135
    }
136
  }
137
}
138