Tag archives for configure

PowerConfig – a global access data manager

While developing you often need to access some data globally: app name, login id, etc.

PHP supplies the $_GLOBAL variabile, CakePHP supplies a more evoluted Configure static class.

CakePower supplies CakeConfig static class that work similar to Configure… but with Power!

The Source :: GitHub Repo

You can look at the fully documented source code in CakePower GitHub repository:

https://github.com/CakePower/CakePower/blob/master/Vendor/PowerConfig.php

Data Tree

PowerConfig handle a data tree as a combination of PHP associative scalar array:

$data = array(
  'key1' => 'a string',
  'key2' => 123,
  'key3' => array( 'green', 'white', 'red' ),
  'key4' => array(
    'subKey' => '...',
    'name-key-as-you-want' => array(),
    'emptyKey' => ''
  )
)

Accessing Data

Accessing data into the tree is easy as you know the path of what you need:

Let me say you need do access $data['key4']['subKey'] value you may write something like this:

PowerConfig::get('key4.subKey'); -> ...
PowerConfig::get('non-existing-key'); -> false
PowerConfig::get('non-existing-key','default value'); -> default value
PowerConfig::get('emptyKey','default value'); -> default value

get( (string)$path, (string)$default )

The get() method can be called with a default value optional param to be returned if no explicit value is available for the given path.

You can also use debug() to apply CakePHP’s* debug()* method to a path:

debug(PowerConfig::get('foo'));
// is equivalent to //
PowerConfig::debug('foo');

You can test for a path existance with:

if ( PowerConfig::exists('foo') ) {
  // path exists
} else {
  // path does not exists
}

You can also set a default value to be used if get value is empty.

// Normal code
if ( PowerConfig::exists('foo') ) {
  echo PowerConfig::get('foo');
} else {
  echo 'default value..';
}

// should be reduced to:
echo PowerConfig::get('foo','default value..');

pval( (string)$path, (string)$default )

pval stands for “param value”.
It works much like get() but searchs key path in a number of places:

  • PowerConfig::get(‘request.params.named.{key}’)
  • PowerConfig::get(‘request.data.{key}’)
  • SessionComponent::read($key)
  • PowerConfig::get($key)
  • $_POST[$key]
  • $_GET[$key]

as get() method you can add a second optional param to be used as default value if no values are found.

This method comes very helpful in situations where your are not sure a param exists:

// Standard code:
if ( isset($this->request->params['name']['foo']) ) {
  echo $this->request->params['name']['foo'];
} else {
  echo "not found!";
}

// Becomes:
echo CakePower::pval('foo','not found!');

CakePower::pval() should be used as shortcut in every CakePHP main objects: Controller, Component, Model, Behavior, Helper as internal public method:

// in a controller:
echo $this->pval('foo','not found!');

debug( (string)$path ), ddebug( (string)$path )

Use it to inspect a path using CakePHP’s debug() utility.

ddebug() will end the script after debugging the path (die-debug).

parent( (string)$path )

Return the parent item of a path.

PowerConfig::parent('foo1.foo2')
 is equvalent to
PowerConfig::get('foo1')</pre>

Writing Data

Here is where PowerConfig become more usefull. You can set data, set default values, extend existing data and more!

set( (string)$path, (mixed)$value )

You can use dotted path notation to write data into the class:

PowerConfig::set( 'foo', 'foo value' ); // foo = "foo value"
PowerConfig::set( 'foo1', array('a','b','c') ); // foo = [a,b,c]

set() method will always overwrite existing data.

def( (string)$path, (mixed)$value )

If you want to set a default value only if the path does not exists or is empty you can use def():

PowerConfig::set( 'nick', 'MPeg' );
PowerConfig::set( 'status', '' );

PowerConfig::def( 'nick', 'luke' ); // return false, "nick" already exists
PowerConfig::def( 'role', 'user' ); // return true, "role" does not exists before
PowerConfig::deg( 'status', 'inactive' ); // return true, "status" was empty

extend( (string)$path, (mixed)$value )

Use this method to save new values into a multidimensional associative array by recursing into it’s structure:

// Set some default data:
PowerConfig::set('test',array(
  'foo1' => 'test string',
  'foo2' => array(
    'sub1' => 23,
    'sub2' => array( 'a', 'b' )
  )
));

// Add new values or overwirte existing data:
PowerConfig::extend('test',array(
  'foo1' => 'modified string',
  'foo2' => array(
    'sub2' => array( 'c' )
  ),
  'foo3' => 'another string'
));

// Now the "test" debug will output:
array(
  'foo1' => 'modified string', // scalar values are overwritten
  'foo2' => array(
    'sub1' => 23,
    'sub2' => array( 'a', 'b', 'c' ), // array are merged
  ),
 'foo3' => 'another string', // new values are appended
)

pushDiff( (string)$path, (mixed)$value )

PowerConfig::pushDiff() works like extend() method but does not overwrite existing data.

Array will be merged, new data will be added but existing scalar data are left untouched.

append( (string)$path, (mixed)$value )

prepend( (string)$path, (mixed)$value )

before( (string)$path, (mixed)$value )

// With associative data:
PowerConfig::set('foo1',array(
  'name' => 'Mark',
  'surname' => 'Sheepkeeper'
));

PowerConfig::before( 'foo1.surname', 'age', '30 );
-> array(
  'name' => 'Mark',
  'age' => 30,
  'surname' => 'Sheepkeeper'
)

// With vectorial data
PowerConfig::set('colors',array( 'red','white','blue' ));

PowerConfig::before( 'colors.white', 'yellow' );
-> [ 'red', 'yellow', 'white', 'blue' ]

PowerConfig::before( 'colors.{2}', 'fuchsia' );
-> [ 'red', 'yellow', 'fuchsia', 'white', 'blue' ]

    

after( (string)$path, (mixed)$value )

It works like PowerConfig::before() but insert new value after the item identified by supplied path.

Deleting Data:

del( (string)$path )

Delete the key content and remove the latest key form the data tree.

clear( (string)$path )

Fill the key content with a null value.

reset()

Destroy all informations in the tree replacing it with an empty array!

Path handling:

thisPath( (string)$path )

parentPath( (string)$path )

Posted in CakePHP | 1 Comment

Swedish Greys - a WordPress theme from Nordic Themepark.