Skip to content

Commit

Permalink
Optimize Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed May 29, 2018
1 parent 48f6ab0 commit d3d0087
Show file tree
Hide file tree
Showing 45 changed files with 485 additions and 441 deletions.
15 changes: 8 additions & 7 deletions src/Containers/Datastructures/SplServiceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Spl\Containers\Datastructures;
Expand All @@ -30,25 +31,25 @@ class SplServiceRegistry extends SplClassInfo
*/
private $instance;

public function __construct( $service )
public function __construct($service)
{
if ( is_object( $service ) ) {
if (is_object($service)) {
$this->instance = $service;
$service = get_class( $service );
$service = get_class($service);
}

parent::__construct( $service );
parent::__construct($service);
}

public function getClassName()
{
return get_class_name( $this->name );
return get_class_name($this->name);
}

public function &getInstance()
{
if ( empty( $this->instance ) ) {
$this->instance = $this->newInstance( func_get_args() );
if (empty($this->instance)) {
$this->instance = $this->newInstance(func_get_args());
}

return $this->instance;
Expand Down
31 changes: 16 additions & 15 deletions src/Containers/SplClosureContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Spl\Containers;
Expand Down Expand Up @@ -40,7 +41,7 @@ class SplClosureContainer implements \Countable, ContainerInterface
* @param string $offset
* @param \Closure $closure
*/
public function attach( $offset, \Closure $closure )
public function attach($offset, \Closure $closure)
{
$this->closures[ $offset ] = $closure;
}
Expand All @@ -57,9 +58,9 @@ public function attach( $offset, \Closure $closure )
*
* @return bool
*/
public function __isset( $offset )
public function __isset($offset)
{
return $this->has( $offset );
return $this->has($offset);
}

// ------------------------------------------------------------------------
Expand All @@ -73,9 +74,9 @@ public function __isset( $offset )
*
* @return bool
*/
public function has( $offset )
public function has($offset)
{
return (bool)isset( $this->closures[ $offset ] );
return (bool)isset($this->closures[ $offset ]);
}

// ------------------------------------------------------------------------
Expand All @@ -88,9 +89,9 @@ public function has( $offset )
*
* @param string $offset
*/
public function __unset( $offset )
public function __unset($offset)
{
$this->detach( $offset );
$this->detach($offset);
}

// ------------------------------------------------------------------------
Expand All @@ -102,10 +103,10 @@ public function __unset( $offset )
*
* @param string $offset
*/
public function detach( $offset )
public function detach($offset)
{
if ( isset( $this->closures[ $offset ] ) ) {
unset( $this->closures[ $offset ] );
if (isset($this->closures[ $offset ])) {
unset($this->closures[ $offset ]);
}
}

Expand All @@ -122,9 +123,9 @@ public function detach( $offset )
*
* @return mixed Returns FALSE when calling the closure is failed.
*/
public function __call( $offset, array $arguments = [] )
public function __call($offset, array $arguments = [])
{
return $this->get( $offset, $arguments );
return $this->get($offset, $arguments);
}

// ------------------------------------------------------------------------
Expand All @@ -139,9 +140,9 @@ public function __call( $offset, array $arguments = [] )
*
* @return mixed Returns FALSE when calling the closure is failed.
*/
public function get( $offset, array $arguments = [] )
public function get($offset, array $arguments = [])
{
return isset( $this->closures[ $offset ] ) ? call_user_func_array(
return isset($this->closures[ $offset ]) ? call_user_func_array(
$this->closures[ $offset ],
$arguments
) : false;
Expand All @@ -163,6 +164,6 @@ public function get( $offset, array $arguments = [] )
*/
public function count()
{
return count( $this->closures );
return count($this->closures);
}
}
71 changes: 36 additions & 35 deletions src/Containers/SplServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Spl\Containers;
Expand Down Expand Up @@ -41,7 +42,7 @@ class SplServiceContainer implements \Countable, ContainerInterface
* @param string $offset
* @param SplServiceRegistry $service
*/
public function attach( $offset, SplServiceRegistry $service )
public function attach($offset, SplServiceRegistry $service)
{
$this->services[ $offset ] = $service;
}
Expand All @@ -56,9 +57,9 @@ public function attach( $offset, SplServiceRegistry $service )
*
* @param string $offset
*/
public function __unset( $offset )
public function __unset($offset)
{
$this->detach( $offset );
$this->detach($offset);
}

// ------------------------------------------------------------------------
Expand All @@ -70,10 +71,10 @@ public function __unset( $offset )
*
* @param string $offset
*/
public function detach( $offset )
public function detach($offset)
{
if ( isset( $this->services[ $offset ] ) ) {
unset( $this->services[ $offset ] );
if (isset($this->services[ $offset ])) {
unset($this->services[ $offset ]);
}
}

Expand All @@ -89,9 +90,9 @@ public function detach( $offset )
*
* @return bool
*/
public function __isset( $offset )
public function __isset($offset)
{
return $this->has( $offset );
return $this->has($offset);
}

// ------------------------------------------------------------------------
Expand All @@ -105,9 +106,9 @@ public function __isset( $offset )
*
* @return bool
*/
public function has( $offset )
public function has($offset)
{
return (bool)isset( $this->services[ $offset ] );
return (bool)isset($this->services[ $offset ]);
}

// ------------------------------------------------------------------------
Expand All @@ -122,9 +123,9 @@ public function has( $offset )
*
* @return bool
*/
public function &__get( $offset )
public function &__get($offset)
{
$get[ $offset ] = $this->get( $offset );
$get[ $offset ] = $this->get($offset);

return $get[ $offset ];
}
Expand All @@ -141,50 +142,50 @@ public function &__get( $offset )
*
* @return mixed Returns FALSE when calling the service is failed.
*/
public function &get( $offset, array $arguments = [] )
public function &get($offset, array $arguments = [])
{
$get[ $offset ] = false;

if ( $this->has( $offset ) ) {
if ($this->has($offset)) {
$service = $this->services[ $offset ];

if ( $service instanceof SplServiceRegistry ) {
if ( empty( $arguments ) ) {
if ($service instanceof SplServiceRegistry) {
if (empty($arguments)) {
return $service->getInstance();
} else {
$newServiceInstance = $service->newInstanceArgs( $arguments );
$newServiceInstance = $service->newInstanceArgs($arguments);

if ( $DocComment = $service->getDocComment() ) {
preg_match_all( '/@inject\s(.*)/', $DocComment, $matches );
if ($DocComment = $service->getDocComment()) {
preg_match_all('/@inject\s(.*)/', $DocComment, $matches);

if ( count( $matches[ 1 ] ) ) {
foreach ( $matches[ 1 ] as $className ) {
$className = trim( $className, '\\' );
$className = trim( $className );
if (count($matches[ 1 ])) {
foreach ($matches[ 1 ] as $className) {
$className = trim($className, '\\');
$className = trim($className);

$map = strtolower( get_class_name( $className ) );
$map = strtolower(get_class_name($className));

if ( isset( $this->map[ $className ] ) ) {
if (isset($this->map[ $className ])) {
$map = $this->map[ $className ];
}

$variable = $map;
$object =& $this->get( $map );
$object =& $this->get($map);

preg_match_all( '/[$](.*)\s(.*)/', trim( $className ), $classMatches );
preg_match_all('/[$](.*)\s(.*)/', trim($className), $classMatches);

if ( count( $classMatches[ 1 ] ) ) {
if (count($classMatches[ 1 ])) {
$variable = $classMatches[ 1 ][ 0 ];
}

$setterMethod = ucwords( str_replace( [ '-', '_' ], ' ', $variable ) );
$setterMethod = str_replace( ' ', '', $setterMethod );
$setterMethod = ucwords(str_replace(['-', '_'], ' ', $variable));
$setterMethod = str_replace(' ', '', $setterMethod);
$setterMethod = 'set' . $setterMethod;

if ( method_exists( $newServiceInstance, $setterMethod ) ) {
$newServiceInstance->{$setterMethod}( $object );
} elseif ( method_exists( $newServiceInstance, '__set' ) ) {
$newServiceInstance->__set( $variable, $object );
if (method_exists($newServiceInstance, $setterMethod)) {
$newServiceInstance->{$setterMethod}($object);
} elseif (method_exists($newServiceInstance, '__set')) {
$newServiceInstance->__set($variable, $object);
} else {
$newServiceInstance->{$variable} =& $object;
}
Expand Down Expand Up @@ -216,6 +217,6 @@ public function &get( $offset, array $arguments = [] )
*/
public function count()
{
return count( $this->services );
return count($this->services);
}
}
31 changes: 16 additions & 15 deletions src/Datastructures/SplArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Spl\Datastructures;
Expand Down Expand Up @@ -36,9 +37,9 @@ class SplArrayObject extends \ArrayObject
*
* @return SplArrayObject Returns an SplArrayObject object on success.
*/
public function __construct( array $array = [], $flag = \ArrayObject::ARRAY_AS_PROPS )
public function __construct(array $array = [], $flag = \ArrayObject::ARRAY_AS_PROPS)
{
parent::__construct( $array, $flag );
parent::__construct($array, $flag);
}

// ------------------------------------------------------------------------
Expand All @@ -52,7 +53,7 @@ public function __construct( array $array = [], $flag = \ArrayObject::ARRAY_AS_P
*/
public function isEmpty()
{
return ( $this->count() == 0 ? true : false );
return ($this->count() == 0 ? true : false);
}

// -----------------------------------------------------------------------
Expand All @@ -66,20 +67,20 @@ public function isEmpty()
*
* @return mixed The value at the specified index or false.
*/
public function __get( $offset )
public function __get($offset)
{
return $this->offsetGet( $offset );
return $this->offsetGet($offset);
}

// ------------------------------------------------------------------------

public function offsetGet( $offset )
public function offsetGet($offset)
{
if ( $this->offsetExists( $offset ) === false ) {
if ($this->offsetExists($offset) === false) {
return false;
}

return parent::offsetGet( $offset );
return parent::offsetGet($offset);
}

// ------------------------------------------------------------------------
Expand All @@ -93,14 +94,14 @@ public function offsetGet( $offset )
*/
public function exchangeOffset()
{
if ( $this->count() > 0 ) {
if ($this->count() > 0) {
$camelcaseStorage = [];

foreach ( $this->getArrayCopy() as $offset => $value ) {
$camelcaseStorage[ camelcase( $offset ) ] = $value;
foreach ($this->getArrayCopy() as $offset => $value) {
$camelcaseStorage[ camelcase($offset) ] = $value;
}

$this->exchangeArray( $camelcaseStorage );
$this->exchangeArray($camelcaseStorage);
}

return $this->getArrayCopy();
Expand All @@ -117,12 +118,12 @@ public function exchangeOffset()
*
* @return array The array merged copy of the resulting array
*/
public function merge( array $values )
public function merge(array $values)
{
$storage = $this->getArrayCopy();
$storage = array_merge( $storage, $values );
$storage = array_merge($storage, $values);

$this->exchangeArray( $storage );
$this->exchangeArray($storage);

return $storage;
}
Expand Down
Loading

0 comments on commit d3d0087

Please sign in to comment.