1+ <?php
2+
3+ namespace Genasyst \phpCompressor ;
4+
5+ use Genasyst \phpCompressor \PhpParser ;
6+ use Genasyst \phpCompressor \NamesRegistry ;
7+ use Genasyst \phpCompressor \PhpParser \Visitors \Traverser \GlobalVisitor ;
8+
9+ use PhpParser \NodeTraverser ;
10+ use PhpParser \ParserFactory ;
11+ use PhpParser \PrettyPrinter ;
12+ use PhpParser \Error ;
13+
14+
15+ class Compressor
16+ {
17+
18+ const COMPRESS_TYPE_LOCAL_VARIABLES = 'local_variables ' ;
19+ const COMPRESS_TYPE_OBJECT_VARIABLES = 'object_variables ' ;
20+ const COMPRESS_TYPE_OBJECT_METHODS = 'object_methods ' ;
21+
22+ protected $ content = '' ;
23+
24+ protected $ whitespaces = false ;
25+
26+ protected $ comments = false ;
27+
28+ protected $ compress_types = [
29+ 'local_variables ' => false ,
30+ 'object_variables ' => false ,
31+ 'object_methods ' => false ,
32+ ];
33+
34+ protected $ exclude_names = [
35+ 'local_variables ' => [
36+ 'this ' => 'this ' ,/** use in Object method scope **/
37+ 'GLOBALS ' => 'GLOBALS ' ,
38+ '_GET ' => '_GET ' ,
39+ '_POST ' => '_POST ' ,
40+ '_FILES ' => '_FILES ' ,
41+ '_COOKIE ' => '_COOKIE ' ,
42+ '_SESSION ' => '_SESSION ' ,
43+ '_SERVER ' => '_SERVER ' ,
44+ 'http_response_header ' => 'http_response_header ' ,
45+ 'php_errormsg ' => 'php_errormsg ' ,
46+ ],
47+ 'object_variables ' => [
48+ 'this ' => 'this ' ,
49+ ],
50+
51+ 'object_methods ' => [
52+ //Magic
53+ 'this ' => 'this ' ,
54+ '__construct ' => '__construct ' ,
55+ '__destruct ' => '__destruct ' ,
56+ '__call ' => '__call ' ,
57+ '__callStatic ' => '__callStatic ' ,
58+ '__get ' => '__get ' ,
59+ '__set ' => '__set ' ,
60+ '__isset ' => '__isset ' ,
61+ '__unset ' => '__unset ' ,
62+ '__sleep ' => '__sleep ' ,
63+ '__wakeup ' => '__wakeup ' ,
64+ '__toString ' => '__toString ' ,
65+ '__invoke ' => '__invoke ' ,
66+ '__set_state ' => '__set_state ' ,
67+ '__clone ' => '__clone ' ,
68+ '__debugInfo ' => '__debugInfo ' ,
69+ // only php 7
70+ 'do ' => 'do ' ,
71+ 'as ' => 'as ' ,
72+ ]
73+ ];
74+ /**
75+ * @var NodeTraverser
76+ */
77+ protected $ traverser = null ;
78+
79+ public function __construct ($ data = [])
80+ {
81+
82+ }
83+
84+ public function compress ()
85+ {
86+ try {
87+ $ statements = $ this ->getStatements ();
88+ $ traverser = $ this ->getTraverser ();
89+ $ traverser ->addVisitor (new GlobalVisitor ($ this ->getCompressSettings ()));
90+ $ new_statements = $ traverser ->traverse ($ statements );
91+ $ this ->content = $ this ->getCompiller ()->prettyPrint ($ new_statements );
92+ } catch (Error $ e ) {
93+ echo 'Parse Error: ' , $ e ->getMessage ();
94+ }
95+ }
96+
97+ public function setExcludeNames ($ type = '' , array $ exclude_names ) {
98+ if (isset ($ this ->compress_types [$ type ])) {
99+ $ this ->exclude_names [$ type ] += $ exclude_names ;
100+ }
101+ }
102+ protected function getCompressSettings ()
103+ {
104+ $ settings = [];
105+ foreach ($ this ->compress_types as $ type => $ enable ) {
106+ $ settings [$ type ] = [
107+ 'enable ' => $ enable ,
108+ 'exclude_names ' => $ this ->exclude_names [$ type ]
109+ ];
110+ }
111+ return $ settings ;
112+ }
113+ public function getContent ()
114+ {
115+ return $ this ->content ;
116+ }
117+
118+ public function getStatements ($ perfer = ParserFactory::PREFER_PHP5 )
119+ {
120+ $ parser = (new ParserFactory )->create ($ perfer );
121+
122+ return $ parser ->parse ($ this ->content );
123+ }
124+
125+
126+ /**
127+ * @return PrettyPrinter\Standard
128+ */
129+ protected function getCompiller ()
130+ {
131+ return new PrettyPrinter \Standard ;
132+ }
133+
134+ protected function getTraverser ()
135+ {
136+ return new \PhpParser \NodeTraverser ();
137+ }
138+
139+ public function removeWhitespaces ()
140+ {
141+ $ this ->whitespaces = true ;
142+ }
143+
144+ public function removeComments ()
145+ {
146+ $ this ->comments = true ;
147+ }
148+
149+ /**********************
150+ * settings
151+ **********************/
152+ public function compressLocalVariablesName ()
153+ {
154+ $ this ->compress_types ['local_variables ' ] = true ;
155+ }
156+
157+ public function compressObjectsVariablesName ()
158+ {
159+ $ this ->compress_types ['object_variables ' ] = true ;
160+ }
161+
162+ public function compressObjectsMethodsName ()
163+ {
164+ $ this ->compress_types ['object_methods ' ] = true ;
165+ }
166+
167+
168+ public function setContentByFile ($ file )
169+ {
170+ if (false === file_exists ($ file )) {
171+ throw new \Exception ('Not found file ' .$ file );
172+ }
173+ return $ this ->content = file_get_contents ($ file );
174+ }
175+
176+ public function setContentByCode ($ code )
177+ {
178+ $ this ->content = $ code ;
179+ }
180+
181+ public function parseBlock ($ blockCode )
182+ {
183+ $ this ->content = '<?php ' . $ blockCode ;
184+ }
185+
186+
187+ }
0 commit comments