8bitOrange

  • Archive
  • RSS
  • Ask me anything

Nerd Post: A utilities component

We are finding often that we want to have cross plugin functionality and sometimes this involves a lot ( a form builder or a messaging system ), sometimes it does not ( a method to list available elements ). So I decided to make a component titled utilities and include it in our app controller by default, so that we would have a single place to gather and use some functionality regardless of which plugin you are inside.

I am posting the utility here with a method called listElements, that can be passed an array with a plugin name and a directory to search, and will return an array of the available files.

/* SVN FILE: $Id$ */

/**
 * Utilities component
 *
 * A place to collect useful methods within the portal
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller.components
 * @since         CakePHP(tm) v 0.10.0.1076
 * @version       $Revision$
 * @modifiedby    $LastChangedBy$
 * @lastmodified  $Date$
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
 
class UtilitiesComponent extends Object {



	/* Function List Elements
	 * Takes an array of options
	 *     plugin = plugin the element is in, if it is in one
	 *     dir = directory inside of the elements directory to search.
	 *
	 * This will navigate to an elements directory grab all available files and return them
	 * in an array with their name minus the extension (ie: view.ctp = view)
	 */
	
	function listElements($options = array())
	{
	
	    App::Import('Core', 'Folder');
		$folder =& new Folder();
		
		//set our option variables and allow plugin to be null.
		$plugin = (isset($options['plugin']))? 'plugins' . DS . $options['plugin'] . DS : null;
		$dir = $options['dir'];
		
		$path = APP . $plugin . 'views' . DS . 'elements' . DS . $dir;
		$tree = $folder->tree($path, true, array('file'));
		
		$elements = array();
		foreach($tree as $item){
		  $item = substr($item, (strrpos($item, '/') + 1), strlen($item));
		  $elements[] = substr($item, 0, strrpos($item, '.'));
		}
	   return $elements;
	
	}

	
}
    • #cakephp
    • #coding
    • #utilities
    • #code
  • 1 year ago
  • 11
  • Permalink
  • Share
    Tweet

11 Notes/ Hide

  1. ballisticexd liked this
  2. dictionaryde2 liked this
  3. miscarriage8it liked this
  4. 8bitorange posted this
← Previous • Next →

About

I'm a nerd lost on the internet. I build things with code and write things with words. Fork me on GitHub

Pages

  • I am Matt Caron and I am addicted to the internet
  • Code Blog
  • Resume
  • Stack Overflow
  • My Photography
  • Nerd Appropriate
  • I'm Google-able

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr