Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Core need some love.
1. Core/Plugin.php: load(): Don't declare $pluginPath in loop.
2. Standardize the code block of pr() and pj()
3. Simple structures use ternary instead of if/else
  • Loading branch information
FrankFFFF committed Mar 28, 2015
1 parent 01c2c72 commit 08aef48
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
7 changes: 1 addition & 6 deletions src/Core/App.php
Expand Up @@ -59,13 +59,8 @@ public static function className($class, $type = '', $suffix = '')
}

list($plugin, $name) = pluginSplit($class);
if ($plugin) {
$base = $plugin;
} else {
$base = Configure::read('App.namespace');
}
$base = $plugin ?: Configure::read('App.namespace');
$base = str_replace('/', '\\', rtrim($base, '\\'));

$fullname = '\\' . str_replace('/', '\\', $type . '\\' . $name) . $suffix;

if (static::_classExistsInBase($fullname, $base)) {
Expand Down
9 changes: 4 additions & 5 deletions src/Core/Configure.php
Expand Up @@ -166,11 +166,10 @@ public static function delete($var)
*/
public static function consume($var)
{
$simple = strpos($var, '.') === false;
if ($simple && !isset(static::$_values[$var])) {
return null;
}
if ($simple) {
if (strpos($var, '.') === false) {
if (!isset(static::$_values[$var])) {
return null;
}
$value = static::$_values[$var];
unset(static::$_values[$var]);
return $value;
Expand Down
7 changes: 1 addition & 6 deletions src/Core/InstanceConfigTrait.php
Expand Up @@ -144,7 +144,6 @@ protected function _configRead($key)
}

$return = $return[$k];

}

return $return;
Expand All @@ -168,11 +167,7 @@ protected function _configWrite($key, $value, $merge = false)
}

if ($merge) {
if (is_array($key)) {
$update = $key;
} else {
$update = [$key => $value];
}
$update = is_array($key) ? $key : [$key => $value];
if ($merge === 'shallow') {
$this->_config = array_merge($this->_config, Hash::expand($update));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Plugin.php
Expand Up @@ -136,8 +136,8 @@ public static function load($plugin, array $config = [])

if (empty($config['path'])) {
$paths = App::path('Plugin');
$pluginPath = str_replace('/', DS, $plugin);
foreach ($paths as $path) {
$pluginPath = str_replace('/', DS, $plugin);
if (is_dir($path . $pluginPath)) {
$config['path'] = $path . $pluginPath . DS;
break;
Expand Down
19 changes: 11 additions & 8 deletions src/Core/StaticConfigTrait.php
Expand Up @@ -72,15 +72,18 @@ trait StaticConfigTrait
*/
public static function config($key, $config = null)
{
// Read config.
if ($config === null && is_string($key)) {
return isset(static::$_config[$key]) ? static::$_config[$key] : null;
}
if ($config === null && is_array($key)) {
foreach ($key as $name => $settings) {
static::config($name, $settings);
if ($config === null) {
// Read config.
if (is_string($key)) {
return isset(static::$_config[$key]) ? static::$_config[$key] : null;
}

if (is_array($key)) {
foreach ($key as $name => $settings) {
static::config($name, $settings);
}
return;
}
return;
}

if (isset(static::$_config[$key])) {
Expand Down
16 changes: 8 additions & 8 deletions src/Core/functions.php
Expand Up @@ -131,10 +131,12 @@ function namespaceSplit($class)
*/
function pr($var)
{
if (Configure::read('debug')) {
$template = PHP_SAPI !== 'cli' ? '<pre class="pr">%s</pre>' : "\n%s\n\n";
printf($template, trim(print_r($var, true)));
if (!Configure::read('debug')) {
return;
}

$template = PHP_SAPI !== 'cli' ? '<pre class="pr">%s</pre>' : "\n%s\n\n";
printf($template, trim(print_r($var, true)));
}

}
Expand All @@ -156,11 +158,9 @@ function pj($var)
if (!Configure::read('debug')) {
return;
}
if (PHP_SAPI === 'cli') {
printf("\n%s\n\n", trim(json_encode($var, JSON_PRETTY_PRINT)));
} elseif (Configure::read('debug')) {
printf('<pre class="pj">%s</pre>', trim(json_encode($var, JSON_PRETTY_PRINT)));
}

$template = PHP_SAPI !== 'cli' ? '<pre class="pj">%s</pre>' : "\n%s\n\n";
printf($template, trim(json_encode($var, JSON_PRETTY_PRINT)));
}

}
Expand Down

0 comments on commit 08aef48

Please sign in to comment.