Skip to content

Commit d3a6214

Browse files
committed
Optimize Codes
1 parent 3912b47 commit d3a6214

15 files changed

+210
-197
lines changed

src/Abstracts/AbstractDriver.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author Steeve Andrian Salim
99
* @copyright Copyright (c) Steeve Andrian Salim
1010
*/
11+
1112
// ------------------------------------------------------------------------
1213

1314
namespace O2System\Parser\Abstracts;
@@ -58,14 +59,14 @@ abstract class AbstractDriver implements ParserDriverInterface
5859
*
5960
* @return bool
6061
*/
61-
public function loadFile( $filePath )
62+
public function loadFile($filePath)
6263
{
63-
if ( $filePath instanceof \SplFileInfo ) {
64+
if ($filePath instanceof \SplFileInfo) {
6465
$filePath = $filePath->getRealPath();
6566
}
6667

67-
if ( is_file( $filePath ) ) {
68-
return $this->loadString( file_get_contents( $filePath ) );
68+
if (is_file($filePath)) {
69+
return $this->loadString(file_get_contents($filePath));
6970
}
7071

7172
return false;
@@ -80,26 +81,26 @@ public function loadFile( $filePath )
8081
*
8182
* @return bool
8283
*/
83-
public function loadString( $string )
84+
public function loadString($string)
8485
{
85-
$this->string = htmlspecialchars_decode( $string );
86+
$this->string = htmlspecialchars_decode($string);
8687

87-
if ( $this->config[ 'allowPhpScripts' ] === false ) {
88+
if ($this->config[ 'allowPhpScripts' ] === false) {
8889
$this->string = preg_replace(
8990
'/<\\?.*(\\?>|$)/Us',
9091
'',
91-
str_replace( '<?=', '<?php echo ', $this->string )
92+
str_replace('<?=', '<?php echo ', $this->string)
9293
);
9394
}
9495

95-
return (bool)empty( $this->string );
96+
return (bool)empty($this->string);
9697
}
9798

9899
// ------------------------------------------------------------------------
99100

100101
public function isInitialize()
101102
{
102-
return (bool)( empty( $this->engine ) ? false : true );
103+
return (bool)(empty($this->engine) ? false : true);
103104
}
104105

105106
// --------------------------------------------------------------------------------------
@@ -111,7 +112,7 @@ public function isInitialize()
111112
*
112113
* @return static
113114
*/
114-
abstract public function initialize( array $config );
115+
abstract public function initialize(array $config);
115116

116117
// ------------------------------------------------------------------------
117118

@@ -122,9 +123,9 @@ public function &getEngine()
122123

123124
// ------------------------------------------------------------------------
124125

125-
public function setEngine( $engine )
126+
public function setEngine($engine)
126127
{
127-
if ( $this->isValidEngine( $engine ) ) {
128+
if ($this->isValidEngine($engine)) {
128129
$this->engine =& $engine;
129130

130131
return true;
@@ -133,16 +134,16 @@ public function setEngine( $engine )
133134
return false;
134135
}
135136

136-
abstract protected function isValidEngine( $engine );
137+
abstract protected function isValidEngine($engine);
137138

138139
// ------------------------------------------------------------------------
139140

140-
public function __call( $method, array $arguments = [] )
141+
public function __call($method, array $arguments = [])
141142
{
142-
if ( method_exists( $this, $method ) ) {
143-
return call_user_func_array( [ &$this, $method ], $arguments );
144-
} elseif ( method_exists( $this->engine, $method ) ) {
145-
return call_user_func_array( [ &$this->engine, $method ], $arguments );
143+
if (method_exists($this, $method)) {
144+
return call_user_func_array([&$this, $method], $arguments);
145+
} elseif (method_exists($this->engine, $method)) {
146+
return call_user_func_array([&$this->engine, $method], $arguments);
146147
}
147148

148149
return null;

src/Abstracts/AbstractEngine.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author Steeve Andrian Salim
99
* @copyright Copyright (c) Steeve Andrian Salim
1010
*/
11+
1112
// ------------------------------------------------------------------------
1213

1314
namespace O2System\Parser\Abstracts;
@@ -28,6 +29,21 @@ abstract class AbstractEngine implements ParserEngineInterface
2829
use FileExtensionCollectorTrait;
2930
use FilePathCollectorTrait;
3031

32+
public function parsePartials($filename, $vars = null, $optionalFilename = null)
33+
{
34+
if (empty($vars)) {
35+
if (isset($optionalFilename)) {
36+
return $this->parseFile($optionalFilename);
37+
}
38+
} else {
39+
return $this->parseFile($filename, $vars);
40+
}
41+
42+
return null;
43+
}
44+
45+
// ------------------------------------------------------------------------
46+
3147
/**
3248
* Base::parseFile
3349
*
@@ -36,24 +52,24 @@ abstract class AbstractEngine implements ParserEngineInterface
3652
*
3753
* @return string
3854
*/
39-
public function parseFile( $filePath, array $vars = [] )
55+
public function parseFile($filePath, array $vars = [])
4056
{
41-
if(class_exists('O2System\Framework', false)) {
57+
if (class_exists('O2System\Framework', false)) {
4258
return view()->load($filePath, $vars, true);
4359
} else {
44-
$fileExtension = '.' . pathinfo( $filePath, PATHINFO_EXTENSION );
60+
$fileExtension = '.' . pathinfo($filePath, PATHINFO_EXTENSION);
4561

46-
if ( in_array( $fileExtension, $this->fileExtensions ) AND is_file( $filePath ) ) {
47-
return $this->parseString( file_get_contents( $filePath ), $vars );
62+
if (in_array($fileExtension, $this->fileExtensions) AND is_file($filePath)) {
63+
return $this->parseString(file_get_contents($filePath), $vars);
4864
}
4965

5066
// Try to find from filePaths
51-
if ( count( $this->filePaths ) ) {
52-
foreach ( $this->filePaths as $fileDirectory ) {
67+
if (count($this->filePaths)) {
68+
foreach ($this->filePaths as $fileDirectory) {
5369
$checkFilePath = $fileDirectory . $filePath;
5470

55-
if ( in_array( $fileExtension, $this->fileExtensions ) AND is_file( $checkFilePath ) ) {
56-
return $this->parseString( file_get_contents( $checkFilePath ), $vars );
71+
if (in_array($fileExtension, $this->fileExtensions) AND is_file($checkFilePath)) {
72+
return $this->parseString(file_get_contents($checkFilePath), $vars);
5773
break;
5874
}
5975
}
@@ -62,19 +78,4 @@ public function parseFile( $filePath, array $vars = [] )
6278

6379
return null;
6480
}
65-
66-
// ------------------------------------------------------------------------
67-
68-
public function parsePartials($filename, $vars = null, $optionalFilename = null)
69-
{
70-
if (empty($vars)) {
71-
if (isset($optionalFilename)) {
72-
return $this->parseFile($optionalFilename);
73-
}
74-
} else {
75-
return $this->parseFile($filename, $vars);
76-
}
77-
78-
return null;
79-
}
8081
}

src/Datastructures/Config.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author Steeve Andrian Salim
99
* @copyright Copyright (c) Steeve Andrian Salim
1010
*/
11+
1112
// ------------------------------------------------------------------------
1213

1314
namespace O2System\Parser\Datastructures;
@@ -26,7 +27,7 @@ class Config extends \O2System\Kernel\Datastructures\Config
2627
*
2728
* @param array $config
2829
*/
29-
public function __construct( array $config = [] )
30+
public function __construct(array $config = [])
3031
{
3132
$defaultConfig = [
3233
'driver' => 'moustache',
@@ -36,8 +37,8 @@ public function __construct( array $config = [] )
3637
'allowPhpGlobals' => true,
3738
];
3839

39-
$config = array_merge( $defaultConfig, $config );
40+
$config = array_merge($defaultConfig, $config);
4041

41-
parent::__construct( $config );
42+
parent::__construct($config);
4243
}
4344
}

src/Drivers.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,6 @@ public function addDriver(Abstracts\AbstractDriver $driver, $driverOffset = null
135135
return $this->__isset($driverOffset);
136136
}
137137

138-
public function getSourceFileDirectory()
139-
{
140-
return $this->sourceFileDirectory;
141-
}
142-
143-
public function getSourceFilePath()
144-
{
145-
return $this->sourceFilePath;
146-
}
147-
148138
public function getSourceString()
149139
{
150140
return $this->sourceString;
@@ -172,8 +162,6 @@ public function loadFile($filePath)
172162
return false;
173163
}
174164

175-
// ------------------------------------------------------------------------
176-
177165
public function loadString($string)
178166
{
179167
$this->sourceString = $string;
@@ -201,6 +189,18 @@ public function loadString($string)
201189
return empty($this->sourceString);
202190
}
203191

192+
public function getSourceFileDirectory()
193+
{
194+
return $this->sourceFileDirectory;
195+
}
196+
197+
// ------------------------------------------------------------------------
198+
199+
public function getSourceFilePath()
200+
{
201+
return $this->sourceFilePath;
202+
}
203+
204204
public function parse(array $vars = [])
205205
{
206206
$output = $this->parsePhp($vars);

src/Drivers/BBCodeDriver.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author Steeve Andrian Salim
99
* @copyright Copyright (c) Steeve Andrian Salim
1010
*/
11+
1112
// ------------------------------------------------------------------------
1213

1314
namespace O2System\Parser\Drivers;
@@ -33,17 +34,17 @@ class BBCodeDriver extends BaseDriver
3334
* @return $this
3435
* @throws \O2System\Core\Exceptions\BadThirdPartyException
3536
*/
36-
public function initialize( array $config )
37+
public function initialize(array $config)
3738
{
38-
if ( empty( $this->engine ) ) {
39-
if ( $this->isSupported() ) {
39+
if (empty($this->engine)) {
40+
if ($this->isSupported()) {
4041
$this->engine = new \JBBCode\Parser();
41-
$this->engine->addCodeDefinitionSet( new \JBBCode\DefaultCodeDefinitionSet() );
42+
$this->engine->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet());
4243
} else {
4344
throw new BadThirdPartyException(
4445
'PARSER_E_THIRD_PARTY',
4546
0,
46-
[ 'BBCode Parser by Jackson Owens', 'https://github.com/jbowens/jBBCode' ]
47+
['BBCode Parser by Jackson Owens', 'https://github.com/jbowens/jBBCode']
4748
);
4849
}
4950
}
@@ -62,7 +63,7 @@ public function initialize( array $config )
6263
*/
6364
public function isSupported()
6465
{
65-
if ( class_exists( '\JBBCode\Parser' ) ) {
66+
if (class_exists('\JBBCode\Parser')) {
6667
return true;
6768
}
6869

@@ -78,9 +79,9 @@ public function isSupported()
7879
*
7980
* @return string
8081
*/
81-
public function parse( array $vars = [] )
82+
public function parse(array $vars = [])
8283
{
83-
$this->engine->parse( $this->string );
84+
$this->engine->parse($this->string);
8485

8586
return $this->engine->getAsHtml();
8687
}
@@ -96,9 +97,9 @@ public function parse( array $vars = [] )
9697
*
9798
* @return bool
9899
*/
99-
protected function isValidEngine( $engine )
100+
protected function isValidEngine($engine)
100101
{
101-
if ( $engine instanceof \JBBCode\Parser ) {
102+
if ($engine instanceof \JBBCode\Parser) {
102103
return true;
103104
}
104105

src/Drivers/BladeDriver.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author Steeve Andrian Salim
99
* @copyright Copyright (c) Steeve Andrian Salim
1010
*/
11+
1112
// ------------------------------------------------------------------------
1213

1314
namespace O2System\Parser\Drivers;
@@ -35,13 +36,13 @@ class BladeDriver extends AbstractDriver
3536
* @return $this
3637
* @throws \O2System\Core\Exceptions\BadThirdPartyException
3738
*/
38-
public function initialize( array $config )
39+
public function initialize(array $config)
3940
{
40-
if ( empty( $this->engine ) ) {
41-
if ( $this->isSupported() ) {
42-
$this->engine = new Blade( $config );
41+
if (empty($this->engine)) {
42+
if ($this->isSupported()) {
43+
$this->engine = new Blade($config);
4344
} else {
44-
throw new BadThirdPartyException( 'PARSER_E_THIRD_PARTY', 0, [ '\O2System\Parser\Engines\Blade' ] );
45+
throw new BadThirdPartyException('PARSER_E_THIRD_PARTY', 0, ['\O2System\Parser\Engines\Blade']);
4546
}
4647
}
4748

@@ -59,7 +60,7 @@ public function initialize( array $config )
5960
*/
6061
public function isSupported()
6162
{
62-
if ( class_exists( '\O2System\Parser\Engines\Blade' ) ) {
63+
if (class_exists('\O2System\Parser\Engines\Blade')) {
6364
return true;
6465
}
6566

@@ -75,9 +76,9 @@ public function isSupported()
7576
*
7677
* @return string
7778
*/
78-
public function parse( array $vars = [] )
79+
public function parse(array $vars = [])
7980
{
80-
return $this->engine->parseString( $this->string, $vars );
81+
return $this->engine->parseString($this->string, $vars);
8182
}
8283

8384
// ------------------------------------------------------------------------
@@ -91,9 +92,9 @@ public function parse( array $vars = [] )
9192
*
9293
* @return bool
9394
*/
94-
protected function isValidEngine( $engine )
95+
protected function isValidEngine($engine)
9596
{
96-
if ( $engine instanceof Blade ) {
97+
if ($engine instanceof Blade) {
9798
return true;
9899
}
99100

0 commit comments

Comments
 (0)