Welcome Guest, Not a member yet? Register   Sign In
Search for a certain function in all helper files?
#1

[eluser]Nummero2[/eluser]
Hi,

me again :-)

I´m trying the following:

-load all helper in my helpers directory
-search them for a certain test function which if its present its always present in the following context: "xxx_helper_test" (xxx represents the various helper names)
-put the results (where the function was found) in a variable and output the _helper_test function in a foreach loop for each helper file with it.

What I got for now: (thankfully because of your help :-))
<?

$helpers = glob(APPPATH.'helpers/*_helper.php'); //so I got my helper files in an array

?>

So the array outputs as "E:/xampp/htdocs/intranet/system/application/helpers/xxx_helper.php"
and so on for all helpers

Now, how can I search the helpers for the function? Can´t find anything corresponding in the php manual :-(

Help?

Greetings Sebastian
#2

[eluser]xwero[/eluser]
The only way to do it it to read the files file by file an search for it using
Code:
preg_match('/^function (.+?)\(/',$line);
As is said in your other thread this is a very expensive operation.
#3

[eluser]sophistry[/eluser]
first of all... what exactly are you trying to acheive? sometimes there are better ways that are not exactly the ways you have thought of. if you state your goal rather than outline your process you might get a better (lightning strike) solution. for instance, maybe your goal is this: "i am building a test harness and i want to make sure every helper function has a corresponding test function." as opposed to "i want to load every file and search for a string."

anyway...
if you have shell_exec (backtick) privileges you can search files faster with
Code:
// not tested
$string = shell_exec("egrep 'function .+_helper_test' /path/to/helpers/*");

or similar to reading the files one-by-one load up all the helpers using the CI loader $this->load->helper('helpername') and then use PHP's function_exists() to see if your test function is there.

cheers.
#4

[eluser]Nummero2[/eluser]
What I want to achieve:

in some of our helper files is a test function implemented as i stated. We now want to build a index file which lists all the cases where this test function is present and make them callable from there automatically.

Example:

Helper files present: test_helper.php (test function impl.) , test23_helper.php (test function impl.) , nontest_helper.php

so my new index should look if the test function is implemented and if so create a new case to call it aswell


Greetings
#5

[eluser]Nummero2[/eluser]
My dode now:

$helpers = glob(APPPATH.'helpers/*_helper.php');

$line = '';

preg_match('/^_helper_test (.+?)\(/',$line);



echo var_dump( $line );

is that correct? For $line I just get an empty string dumped on the screen. Also I can´t find an explanation for the used function in detail (.+?)etc.

Besides that what does that do know. Searching all helper files for the function and put them into the variable $line?
#6

[eluser]sophistry[/eluser]
you replaced the string "function" with "_helper_test" in the preg_match() pattern that xwero provided. that is wrong. he gave you the pattern and you changed it - i guess because you thought he meant "replace the word 'function' with the name of the function."

copy and paste his preg_match() function verbatim.
#7

[eluser]Nummero2[/eluser]
Yes thats the point. I´m not getting through the whole meaning of the preg_match function

-Namely where do I then define the function I am searching for? I mean where do I define that it should search the helper files for the _helper_test function if present?

-what is the variable $line for?

Heres my code now but it only shows a one line string like
string(74) "E:/xampp/htdocs/intranet/system/application/helpers/one_helper.php"

Code:

$helpers = glob(APPPATH.'helpers/*_helper.php');

$i=0;
foreach ($helpers as $line) {


preg_match('/^function (.+?)\(/',$line);
$i++;
}


echo var_dump( $line );


Sorry if the whole topic in itself is not reasonable but at the moment I´m just playing around with different ideas/methods.

Greetings Sebastian
#8

[eluser]sophistry[/eluser]
yes, that's right so far.

what you see after the foreach loop is the path to the last helper in your helpers directory.

xwero left out the $matches_array you need in the preg_match() function.

preg_match() is a weird function because you send it an undeclared variable and it makes an array for you.

your preg_match() should look like:
Code:
preg_match(’/^function (.+?)\(/’,$line,$matches_array);

and then inside the foreach loop you need to save the matches array in yet another array.

try the var_dump inside the foreach to see what's really happening and you should get the idea.
#9

[eluser]Nummero2[/eluser]
Thanks but still no luck, now I´m getting the following as a dump in firefox:
"array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }"

Code:

$helpers = glob(APPPATH.'helpers/*_helper.php');

$i=0;
foreach ($helpers as $line) {


preg_match('/^function (.+?)\(/',$line,$matches_array);
$test = $matches_array;
echo var_dump( $test );
$i++;
}

What I still don´t get is where am I defining for what function syntax namely 'xxx_helper_test' I´m searching? So now it seems reasonable to me that i am just getting empty arrays because he doesn´t even know for what he should search.

Greetings Sebastian
#10

[eluser]sophistry[/eluser]
believe it or not, you are getting closer...

first of all, please post code with code blocks so it shows up with indents and syntax highlighting in the forum.

regex is a strange and wonderful thing. This is the thing that is "capturing" your function name.
Code:
(.+?)

The fact that the $matches_array has nothing in it means that something is wrong with the regex. It's probably the caret sign
Code:
^

that tells the regex to only look for the string "function" when it starts exactly at the start of a newline. probably not working because you have some spaces in there. try removing the caret and see what you get.

also, you don't need the $i - it isn't doing anything.

try this:
Code:
$helpers = glob(APPPATH.'helpers/*_helper.php');
foreach ($helpers as $line) {
// removed caret
preg_match('/function (.+?)\(/',$line,$matches_array);
echo var_dump( $matches_array );

}

if that regex doesn't work you can try this one:
Code:
preg_match('/function([^\(]+)/',$line,$matches_array);

that's a little different. it tells the regex engine to look for the string function and then captures anything up to the opening parentheses to the right of the function name.




Theme © iAndrew 2016 - Forum software by © MyBB