A beginner diving into Joomla! core!!

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
ah72
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sat Mar 15, 2008 9:43 pm

A beginner diving into Joomla! core!!

Post by ah72 » Tue Mar 18, 2008 2:38 pm

Links : A beginner diving into Joomla! core!! : Part 1 Part 2

Hello everybody,

I’m new to Joomla! To php and to web programming and design, after learning some basics on html/css, php/MySQL, I wanted to know how Joomla! works and how information is gathered from different sources in Joomla! and displayed to the user (data flow), I was looking for an easy and quick way to understand that, I googled, searched in Joomla developer site, forum, unfortunately couldn’t find my way inside the huge stack of docs out there, so I decided to cut it short and jump directly into the core code of Joomla!, it was an exciting trip, and I wanted to share this experience with you, so jump with me in the car and let’s go for the ride!!

What you need:
  • Basic knowledge on html, php and oop (object oriented programming)
  • Good text editor with php syntax coloring (I use Komodo)
  • Php manual (from here)
  • Joomla! Framework API documentation (from here)
  • Fresh Joomla! 1.5 installation in your local server with default settings and sample data.
You don’t need to know how to use Joomla! and administer it, I began my trip with very little knowledge on that, but after getting the big picture on how Joomla! core works you’ll shorten a lot your way to being a powerful user.

As we move along this ride we’ll find lots of intersections leading to multiple paths, we won’t choose the very specific paths instead we’ll take say the “most used” path and continue driving.

Parking brake is off...get ready!!

STEP 1:
Open your joomla site in your browser
Open the index.php in your text editor (you’ll find it in the root folder of Joomla! this folder will be the reference for our future file and path referring)
Take a general look at it…
Some defines, includes, and other bizarre class functions!! and at the end there is an echo statement, try to comment it…refresh your browser…page displays nothing! So now we need to explore this echo statement, uncomment it and add this line above it:

Code: Select all

echo "STEP 1: Exploring: JResponse::toString(\$mainframe->getCfg('gzip'))<br/><br/>"; 
refresh browser…you can see now your echoed text but in a dark blue background! let's change this background, go to joomla admin page > menu extensions > Template manager > select rhuk_milkyway > press Edit button > change Background Variation to white

Let’s go to the API doc to look for JResponse::toString()
(alternatively you can use this site to search for classes, functions, variables… in Joomla framework)
Select Joomla-Framework from the drop down list, Element index, letter J and click on JResponse… read its description…where it is located(Note: folder “joomla” is inside “libraries”)… and take a general look at its “Method Summary” (from now on we’ll refer to this procedure by: examine the class…)
Now click on method toString from “Method Summary”…read its description, arguments, return value etc …(we’ll refer to this procedure by: examine the method…)

It states that the job of this method is sending headers and returning a string, we need to know which headers are sent and more important which string it returns! As this string would be the whole html of our page. So now we need to explore in depth this method, but before that :

Let’s take a look at the headers sent to the browser, you remember a method called getHeaders() when you examined the class JResponse don’t you! Let’s try using it…replace this code:

Code: Select all

echo "STEP 1: Exploring: JResponse::toString(\$mainframe->getCfg('gzip'))<br/><br/>";
echo JResponse::toString($mainframe->getCfg('gzip')); 
with this:

Code: Select all

$site_index_page_html = JResponse::toString($mainframe->getCfg('gzip'));

echo "<span class='componentheading'>STEP 1: Exploring: JResponse::toString(\$mainframe->getCfg('gzip'))</span><br/><br/>";
echo "<b>All sent headers are: </b>"; print_r(headers_list()); echo "<br/><br/>";
echo "<b>Headers sent by Joomla are: </b>"; print_r(JResponse::getHeaders()); echo "<br/><br/>";
echo "( Note: for a better viewing of arrays, look at the page source (Ctrl+u in firefox) )<br/><br/>";

echo $site_index_page_html; 
also we’ve read from the API doc that toString() accepts an argument (boolean $compress: If true, compress the data), let’s check the value of our passed argument $mainframe->getCfg('gzip')
Add this code before echoing $site_index_page_html:

Code: Select all

echo "<b>\$mainframe->getCfg('gzip') is: </b>"; var_dump($mainframe->getCfg('gzip')); echo "<br/>"; 
refresh browser… string with value “0” isn’t it! (should have the same effect of "false", meaning no data compression)

As this post is getting lengthy, we'll stop here and continue on another post where we'll discuss the following points:
  • investigating source code of "toString()"
  • You can see that our index.php is getting messy with our html echoing, to avoid this (in index.php and other files) we'll build a simple class in a separate file to handle that echoing, this class will be converted later on to a module that will represent our learning (and in the same time debugging) tool
I hope you enjoyed this first part of the ride! make sure everything is clear before we continue, if you have a question on what we've seen don't hesitate

Waiting for your feedback and comments, see u in part 2...

Links : A beginner diving into Joomla! core!! : Part 1 Part 2
Attachments
modified_index.php.zip
(1.19 KiB) Downloaded 36 times
Last edited by ah72 on Thu Mar 20, 2008 9:29 am, edited 7 times in total.

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: [TUTORIAL] A beginner diving into Joomla! Core!! part 1

Post by Rogue4ngel » Wed Mar 19, 2008 12:50 am

A very interesting journey indeed. Thanks for the contribution. Look forward to seeing what other little tidbits you have to offer.
If you're not a part of the solution, you're a part of the problem.

ah72
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sat Mar 15, 2008 9:43 pm

Re: A beginner diving into Joomla! core!!

Post by ah72 » Wed Mar 19, 2008 12:53 pm

Thx a lot for your reply Rogue4ngel,

as I'm new to the Joomla! world I'd like you follow along with me as I write these guides and point me to any errors, I'll also ask for your help when I'm stuck! thx in advance

I wonder if someone could transform these posts to video tutorials and work in parallel with me, this way we'll contribute a lot to growing the Joomla! developers community
Last edited by ah72 on Thu Mar 20, 2008 9:25 am, edited 3 times in total.

ah72
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sat Mar 15, 2008 9:43 pm

A beginner diving into Joomla! core!! part 2

Post by ah72 » Wed Mar 19, 2008 1:30 pm

Links : A beginner diving into Joomla! core!! : Part 1 Part 2

Welcome to "Part 2" of this ride!

Someone could ask this question when reading "part 1" : how come "index.php" knows about our discussed class JResponse... the answer would be : the included files from the beginning of its code, investigating how is that done might be in another ride!

Let's open "response.php" the file containing "JResponse::toString()" (which is -from the API doc- in this folder: libraries/joomla/environment)
navigate to line 188 (as it says in the API doc)...take a general look at this method: assigning "JResponse::getBody()" to a variable...some conditional statements...sending headers...and finally returning the previous variable "$data"
Don't let your attention be diverted to the details of the conditional statements and what's inside them, instead we would look first for the the most important bit which is the return value in this case, once we get comfortable with that and we grasp the general purpose of the method we can then go ahead and inspect other parts by order of importance (which is not necessarily the order of statements in the method)

Recap: "toString()" returns "JResponse::getBody()" compressed (if a certain condition is true), sets headers for no caching (if cache is not allowed) and sends them.

Our next step should be to study either the headers part or "JResponse::getBody()", we'll choose as you might guess "JResponse::getBody()"; Before that we need to display in our browser what was going on inside the method "toString()"

Instead of adding "echo"s, let's gather the text we want to display from this method in a variable and echo it when we want, we'll call that variable "$msg", and why not make it part of a new class that will handle all our learning needs, this way we can develop it as we move along separately from Joomla code,
we might imagine a pattern like this for our new class and variable:
JomDbg::msg['JResponse']['toString']['msg'] = "My name is toString and I've been executed...blah blah blah";
and apply this pattern to gather information from other joomla classes and methods as they are executed
One of you might ask: what if a method is executed more than once? we'll get only the last msg from it as the previous ones are being overwritten, the answer would be to add a counter and correct the JomDbg::msg pattern to this:
JomDbg::msg['JResponse']['toString']['msg'][$pass_counter]

Easy, isn't it! let's try it; Add this code at the first line inside the function toString():

Code: Select all

static $pass_counter = 1;   
Add this code before the last line "return $data;" :

Code: Select all

JomDbg::$msg['JResponse']['toString']['msg'][$pass_counter] = 
"<b>Method toString() execution Report: </b>"
."headers sent". ( JResponse::allowCache() === false ? "(Caching disabled) " : "(Caching enabled) " )
."will return JResponse::getBody()".( ($compress && !ini_get('zlib.output_compression') && ini_get('output_handler')!='ob_gzhandler')?
"(compressed)" : "(without compression)" );

$pass_counter++;   
Let's now create the class JomDbg: create a new file, save it as "JomDbg.php" in your site's main directory, add this code to it :

Code: Select all

<?php
class JomDbg {
    static $msg = array();
}   
(Note: the closing php tag is not necessary)

Let's include it in our index.php: locate this code:

Code: Select all

require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );   
Add this code after it:

Code: Select all

require_once ( JPATH_BASE .DS.'JomDbg.php' );   
Let's now add some code in our index.php to display the data gathered from our new class JomDbg:
Add this code before "echo $site_index_page_html;" :

Code: Select all

echo "<br/><br/>";
echo "<span class='componentheading'>STEP 2: Studying toString() and Building JomDbg Class</span><br/><br/>";
print_r(JomDbg::$msg); echo "<br/><br/>";   
refresh browser...and Voila! a function execution report is displayed, congratulations for making your first step of building your Joomla learning tool, the display though is not as pretty as we want, so the next step would be to make a method for our class JomDbg to handle displaying messages, try to make your own method and post it before we move on to part 3 of this ride, I'll show you what I come up with when part 3 gets ready...stay tuned!

Links : A beginner diving into Joomla! core!! : Part 1 Part 2
Attachments
Modified_files_Part2.zip
(3.86 KiB) Downloaded 32 times
Last edited by ah72 on Thu Mar 20, 2008 9:30 am, edited 5 times in total.

ah72
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sat Mar 15, 2008 9:43 pm

Re: A beginner diving into Joomla! core!!

Post by ah72 » Wed Mar 19, 2008 1:31 pm

Here is an overview of what we'll discuss on our next trip inside Joomla!:

Enhancing our learning tool the class "JomDbg"

Studying "JResponse::getBody()" : what it returns, and if this is a getter method what is the setter method, obviously it will be "setBody()" and what was the function that used it to assign the page html to the body of JResponse

Stay tuned...


Post Reply