CodeIgniter Forums

Full Version: Read/Load all php files with a certain ending?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3

El Forum

[eluser]Nummero2[/eluser]
Hi,

maybe stupid question, sorry if so.

I have a lot of personal helper files in my system/application/helpers folder. They all end in a certain way 'x_help.php'. For example:
- PERSONALx_help.php
- STAFFx_help.php
- RETURNx_help.php
and so on

Now I want to build a class to show them all automatically in my sidekick and whenever I create a new helper I want that it adds up on its own.

So does anyone of you know how I can load all files with my certain ending?

Has to be something like this I guess:

"if( file_exists( $this->load->helper('???x_help.php'));"

Help :-(?

Greetings Sebastian

El Forum

[eluser]SpooF[/eluser]
Place this below the $autoload['helper'] = array('');

Or if you want this in a contructor you can change it a bit to use the $this->load->helper() function. Let me know if you need help to alter it.

Code:
$handle = opendir(APPPATH.'helpers');

if ( $handle )
{
        while ( false !== ($helper = readdir($handle)) )
        {
                // make sure we don't map silly dirs like .svn, or . or ..

                if ( substr($helper, 0, 1) != "." )
                {
                    $pattern = '/^([a-zA-Z0-9])+(_help\.php)/'; # This should work, you can edit `_help\.php` to change the ending tag of your file, it currently matches anything with _help.php on the end.
                    if ( preg_match($pattern,$helper) )
                    {
                        $h_array = substr($helper,0,-4);
                    }
                }
        }
}

$autoload['helper'] = array_push($autoload['helper'],$h_array);

Basically what this will do is read your helpers folder for all its files, then it will check to see if the file name matches your format. If it does it adds it to an array and after the script goes through all the files it combines it with what ever other helpers you already have selected to load.

I'll see if I can get back to you on the expression. I just need to find the manual online.

El Forum

[eluser]Hannes Nevalainen[/eluser]
use the extension myLittleHelper_helper.php then you use the standard CI $this->load->helper() function.

Regards

El Forum

[eluser]SpooF[/eluser]
Will that load all helper files with the format *_helper.php? I looked through the load helper function and didn't see any code to do it.

El Forum

[eluser]Hannes Nevalainen[/eluser]
Nope it won't. But you can specify all in the autoload config file =)
I think it's the best way to do it. Aviod hacking the core files as much as possible will make life easier for you when it's time to update the core files. =)

Happy Coding!

El Forum

[eluser]xwero[/eluser]
The helper filenames are supposed to have the extension _helper.php so i think you get errors loading the files.

If you prefix the extension for example like this ; _autoload_helper.php , and adapt SpooFs solution of a directory walk and regex a bit and everything should work as you want.

El Forum

[eluser]Bramme[/eluser]
Look into the php glob function. should help you pretty good.

El Forum

[eluser]xwero[/eluser]
nice find Bramme! The only problem is that it returns the full filepath where the autoloader only requires the filename part before the _helper part. So you have to process the array values before you can use it as the helper autoload array.

Code:
$files = glob(APPPATH.'helpers/*_autoload_helper.php');
foreach($files as $key=>$file)
{
   $files[$key] = str_replace('_helper.php','',substr(strrchr($file,'/'),1));
}

$autoload['helper'] = $files;

El Forum

[eluser]Bramme[/eluser]
I've always heard that strpos (or strrpos for that matter) are better/faster alternatives then srrchr.

Should do some benchmarks though.


Edit: okay, done some benchmarks (my first actually :p)

I searched for the last occurence of the character 'j' in a paragraph of lorem ipsum text. Repeated it 100.000 times, strrpos was marginally faster: about 0.005-0.006 seconds, however, when searching for 'dj' (which doesn't occur in the paragraph) strrpos suddenly takes a full 2 more seconds.

El Forum

[eluser]xwero[/eluser]
I'm so used to strrchr when i need a string part but strrpos can be used too.

Code:
$files[$key] = str_replace('_helper.php','',substr($file,strrpos($file,'/')+1));

I found a benchmark here. Because your not going to have much files, i think, i'm not sure if the performance hit with strrchr will matter. As you see in the blog post the iteration is 10000. I would like to see the benchmark for shorter strings and fewer iterations.
Pages: 1 2 3