Part of the EllisLab Network
   
3 of 6
3
Extending Core Libraries in System Folder
Posted: 24 April 2007 04:58 PM   [ Ignore ]   [ # 31 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Something to mention is that the ci_find_resource() function acts in a cascading manner.  If you know much about CSS this will make perfect sense to you.  In CSS the most localized declarations take precedence over the more remote ones.  So an inline style takes precedence over a style block in the head of a page which takes precedence over an external style sheet.

It is the same in this modified CI.  Configurations/Libraries/Views/etc in the local application directory will be loaded in preference to those in the globally accessible extended directories which are loaded in preference to the ones in the CI system dir.  If you configure more than one directory in the ci_search_paths config, they are given preference in the order they appear in the array.

So if you were to configure a single directory in ci_search_paths and had a library file named mylibrary.php in the local applications/libraries directory, the configured search directory’s libraries directory, and the system/libraries directory, only the one from the local application would get loaded.  However, if you were to remove the one in the local application directory and leave the other two, the one from the configured directory would now load.

Profile
 
 
Posted: 24 April 2007 05:41 PM   [ Ignore ]   [ # 32 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

Exactly as it should be. Good job.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 25 April 2007 08:01 AM   [ Ignore ]   [ # 33 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  7337
Joined  03-23-2006

Jim, this looks wonderful.  At this time I’m not able to set it up and test locally, but I’ll try to find time for it shortly.  I’d sincerely like to see the whole community test it also. 

Can you think of any drawbacks/limitations to this?

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 25 April 2007 08:05 AM   [ Ignore ]   [ # 34 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007
Derek Allard - 25 April 2007 08:01 AM

Can you think of any drawbacks/limitations to this?

The only one I can think of is what I mentioned in the comments on the config section.  That is that the more custom entries you put into this, the slower your CI system could become when loading things that are “higher” up in the “search cascade”.

However, I don’t think there are ANY performance impacts if nothing is configured into the custom search paths because ci_find_resource() will only look in APPPATH and BASEPATH ase it already does for everything anyway.  Adding a few entries shouldn’t make much impact at all, especially if a file is found fairly “low” in the “search cascade”.

Profile
 
 
Posted: 25 April 2007 11:20 AM   [ Ignore ]   [ # 35 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

Jim, I can’t get your 1.5.3.modLoader to work on Win XP. I haven’t ruled out my own incompetence yet, but while debugging I tried to incorporate an error message in set_search_paths():

if ( ($path=realpath($path))!==FALSE && is_dir($path) )
            
{
                $search_paths[]
= $path.'/';
            
}
       
else   
           
{                 
             log_message
( 'error', 'Invalid path in config: '.$path ) ;
          
}

Somehow this caused set_search_paths() to fail, thus in turn causing ci_find_resource() to fail on the foreach as $SPATH wasn’t set. All this suggests that more robustness is needed.
I’ll carry on testing - my own incompetence is usually suspect number one….

Profile
 
 
Posted: 25 April 2007 11:27 AM   [ Ignore ]   [ # 36 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Thanks Martin!  Do you believe this could be related to the different slash notations used by different OSes?  I wrote and tested only on Linux machines as I do not have an environment on Windows.

Perhaps it is also related to Windows style backslashes within double quotes in your ci_search_paths configuration?

Thanks again,
Jim

Profile
 
 
Posted: 25 April 2007 11:32 AM   [ Ignore ]   [ # 37 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006
JAAulde - 25 April 2007 11:27 AM

Thanks Martin!  Do you believe this could be related to the different slash notations used by different OSes?  I wrote and tested only on Linux machines as I do not have an environment on Windows.

Perhaps it is also related to Windows style backslashes within double quotes in your ci_search_paths configuration?

Thanks again,
Jim

The OS will be suspect number two! See you later….

Profile
 
 
Posted: 25 April 2007 12:48 PM   [ Ignore ]   [ # 38 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Martin, it appears as if this is my fault for not thinking about Windows…

The problem is in this section of set_search_paths()

$path =
  
substr($path,0,1)=='/'?
    
$path:
    
SELF.$path;

That section looks to see if the path starts with a slash and if it does, sets the path to the configured string, if it doesn’t sets the path to the absolute path of SELF with the configured path appended.  I was thinking only in terms of *nix and thinking that if the path starts with a slash, it is already absolute.  If it did not start with a slash it was relative and I made it absolute for reliability.  However, in Windows, an absolute path starts with a drive letter followed by a colon.

There was also an issue where I made a mistake on what the constant SELF actually held and I have worked around that as well.

The modified set of lines looks like this:

$path =
  (
substr($path,0,1)=='/' || substr($path,1,1)==':')?
    
$path:
    
realpath(dirname(SELF)).'/'.$path;

Slash direction seems to be of no matter in PHP.

Updated with change log here: CodeIgniter_1.5.3_modLoader

Profile
 
 
Posted: 26 April 2007 11:16 AM   [ Ignore ]   [ # 39 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006
JAAulde - 25 April 2007 12:48 PM

.... I was thinking only in terms of *nix and thinking that if the path starts with a slash, it is already absolute.  If it did not start with a slash it was relative and I made it absolute for reliability.  However, in Windows, an absolute path starts with a drive letter followed by a colon.

There was also an issue where I made a mistake on what the constant SELF actually held and I have worked around that as well.

Good calls. I’ve at least got a path included in the $SPATH array now, but CI still doesn’t find the relevant file.
I’m looking at ci_find_resource() now:

if ( (is_array($exclude_in_search) && !in_array($spath,$exclude_in_search)) &&  is_file($spath.$resource_subdir.'/'.$resource_file) )
        
{
            $returnVal
= $spath.$resource_subdir.'/'.$resource_file;
            break;
        
}

In the case of an absolute path, for instance, why would you interpose $resource_subdir between the path and the filename?
Latest news: my browser(s) have started insisting on searching for www.localhost.com when I try to access your CI install. Any other “http://localhost” request is granted, but your CI - even after renames and reinstalls - returns “www.localhost.com could not be found”. I’ve rebooted xampp, rebooted the machine, no joy. Really weird. Not your problem, I’m just telling you so you don’t expect more test reports for a while.

Profile
 
 
Posted: 26 April 2007 11:44 AM   [ Ignore ]   [ # 40 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

In the case of an absolute path, for instance, why would you interpose $resource_subdir between the path and the filename?

This is something that proper documentation would help with.  I tried to make it clear, but failed… :(

It would be better stated for me to say that the configuration is a list of absolute or relative paths to a parent directory of CI resource directories.

So…let’s say I have the following directory on my machine which I want to use as one of the search paths: /var/www/global_ci_extend I make that directory and configure that path into the new config option.

But I want to organize code that I put in there in the same manner that code is organized in the system and appplication directories…so I make subdirectories under /var/www/global_ci_extend/ such as config/, libraries/, etc.  Giving me the following structure:

var/
  |
  |-
www/
    |
    |-
global_ci_extend/
      |
      |-
config/
      |
      |-
errors/
      |
      |-
helpers/
      |
      |-
hooks/
      |
      |-
language/
      |
      |-
libraries/
      |
      |-
models/
      |
      |-
plugins/
      |
      |-
scripts/
      |
      |-
views/

Now write a custom library and I put it in /var/www/global_ci_extend/libraries/.

I call $this->load->library(‘libname’).  Loader::library() takes ‘libname’ and capitalizes it and appends the extension (.php) to it.  Then Loader::library passes that file name to ci_find_resource() along with the resource subdirectory name of ‘libraries’.

ci_find_resource() begins appending the subdirectory and filename to entries in $SPATHS and looks to see if it exists.  $SPATHS always has APPPATH and BASEPATH in it (first and last respectively) which are absolute paths to system/ and application/ (say, /var/www/docroot/system and /var/www/docroot/system/application).  With the configured entry you would also now have /var/www/global_ci_extend between those two in $SPATHS.

So now ci_find_resource() gets a call with the arguments ‘Libname.php’ and ‘libraries’.  It begins looping through $SPATHS assembling a file path and checking for existence.  It first tries the local application:
/var/www/docroot/system/application/libraries/Libname.php
This doesn’t exist so it moves onto the configured directory:
/var/www/global_ci_extend/application/libraries/Libname.php
This exists because I put it there, so that full path is returned for inclusion.

I hope that make sense…

Profile
 
 
Posted: 26 April 2007 12:17 PM   [ Ignore ]   [ # 41 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Hi Jim
Just wanted to publicly say a big thank you grin for all your work on this.
I have just downloaded the mod and am only starting to test now.

I’ll be testing on windows and Ubuntu Linux, with PHP 4 and 5.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 26 April 2007 12:19 PM   [ Ignore ]   [ # 42 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1749
Joined  06-23-2006

Yes, has been clear to me all along, JAAulde. Keep up the good work.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 26 April 2007 02:19 PM   [ Ignore ]   [ # 43 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1156
Joined  08-06-2006

Some other topics where these issues are discussed:
Setting up CI with virtual hosts in Apache
Sharing CI code with multiple applications
Non-application-specific CI system libraries
Better resource handling (with patches)

Related topics that mention this idea:
Discussion of multiple apps with side discussion of multi-domain setup
CI Roadmap discussion with aside about this key feature being discussed

Of course, here is the link to this thread itself which seems to be the latest push to get this key development feature into the CI core:
Extending core libraries in System Folder—I put this link here so this thread itself will show up in searches for it!

Finally, a plug cheese for a feature request I just put in that describes a simple new forum feature that could be useful in gathering these related topic lists:
http://www.codeigniter.com/forums/viewthread/51212/

 Signature 

imap_pop get email | site_migrate port sites | OOCalendar | PhotoBox2 gallery | CI/EE 2 word_limiter, yep, wrote it

Profile
 
 
Posted: 26 April 2007 03:17 PM   [ Ignore ]   [ # 44 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006
JAAulde - 26 April 2007 11:44 AM

....I hope that make sense…

Yes, thanks for spelling it out.
@coolfactor: have you tested this?

Profile
 
 
Posted: 27 April 2007 04:52 AM   [ Ignore ]   [ # 45 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

OK Jim, I can now confirm this is working on Win XP (although I’ve only used a one-element config path array so far).
I originally tried to use a sub-directory of system/libraries for my own generic library classes, which on detailed investigation of the code was obviously not going to work. I now have CodeIgniter/my_system/.... which works fine. Thank you again.
Any more testers stepping up here?

Profile
 
 
   
3 of 6
3
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 10:15 AM
Total Registered Members: 119706 Total Logged-in Users: 35
Total Topics: 125901 Total Anonymous Users: 1
Total Replies: 662431 Total Guests: 414
Total Posts: 788332    
Members ( View Memberlist )