meet magento belarus debug pavel novitsky (eng)

46
Pavel Novitsky Meet Magento Belarus 2012 Magento Debug Process

Upload: pavel-novitsky

Post on 18-May-2015

1.406 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Meet Magento Belarus debug Pavel Novitsky (eng)

Pavel Novitsky

Meet Magento Belarus 2012

Magento Debug Process

Page 2: Meet Magento Belarus debug Pavel Novitsky (eng)

Debugging is twice as hard as writing the code in the first place.

Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

Brian Kernighan

Page 3: Meet Magento Belarus debug Pavel Novitsky (eng)

The main thing about Magento is PHP

Page 4: Meet Magento Belarus debug Pavel Novitsky (eng)

Popular practices of PHP applications debugging.

1. Error output

2. Variable values

3. Structured data

4. Code tracing

5. Objects analysis

6. Database query

Page 5: Meet Magento Belarus debug Pavel Novitsky (eng)

Error output

In php.ini:

display_errors = Onerror_reporting = E_ALL | E_STRICT

Directly in the application:

ini_set('display_errors', 'On');error_reporting(E_ALL | E_STRICT);

Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query

Popular practices of PHP applications debugging.

Page 6: Meet Magento Belarus debug Pavel Novitsky (eng)

Variable value output

echo $myVar;

Virtually useless.

In most cases — a senseless waste of time.

Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query

Popular practices of PHP applications debugging.

Page 7: Meet Magento Belarus debug Pavel Novitsky (eng)

Structured data output

var_dump($myArray);echo '<pre>'.print_r($myArray, true).

'</pre>';

Array( [key1] => value 1 [key2] => value 2)

Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query

Popular practices of PHP applications debugging.

array(2) { ["key1"]=> string(7) "value 1" ["key2"]=> string(7) "value 2" }

Page 8: Meet Magento Belarus debug Pavel Novitsky (eng)

Code tracing

debug_backtrace() and print_debug_backtrace()

Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query

Popular practices of PHP applications debugging.

Page 9: Meet Magento Belarus debug Pavel Novitsky (eng)

What object do we have?

get_class()

get_class_vars()

get_declared_classes()

method_exists()

Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query

Popular practices of PHP applications debugging.

Page 10: Meet Magento Belarus debug Pavel Novitsky (eng)

Database query

a) echo $sql = "SELECT `some_field1` FROM `table` WHERE `some_field2` = '$var'";

b) $res = mysql_query($sql) or die(mysql_error());

Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query

Popular practices of PHP applications debugging.

Page 11: Meet Magento Belarus debug Pavel Novitsky (eng)

It's good enough for a majority of applications.

But what about Magento?

Page 12: Meet Magento Belarus debug Pavel Novitsky (eng)

error_reporting(E_ALL | E_STRICT);ini_set('display_errors', 1);

if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) { Mage::setIsDeveloperMode(true);}

addSetEnv MAGE_IS_DEVELOPER_MODE “true”to .htaccess

Popular practices of PHP applications debugging.

Error output

Page 13: Meet Magento Belarus debug Pavel Novitsky (eng)

For intermediate variable verification only.

Still useless.

Standard practices used in Magento

Variable value output

Page 14: Meet Magento Belarus debug Pavel Novitsky (eng)

$customer = Mage::getModel('customer/customer')->load(1);

print_r($customer);

Standard practices used in Magento

Structured data output

Page 15: Meet Magento Belarus debug Pavel Novitsky (eng)

Mage_Customer_Model_Customer Object ( [_eventPrefix:protected] => customer [_eventObject:protected] => customer [_errors:protected] =>

Array ( ) [_attributes:protected] => [_addresses:protected] => [_addressesCollection:protected] => [_isDeleteable:protected] => 1 [

_isReadonly:protected] => [_resourceName:protected] => customer/customer [_resource:protected] => [_resourceCollectionName:protected] =>

customer/customer_collection [_cacheTag:protected] => [_dataSaveAllowed:protected] => 1 [_isObjectNew:protected] => [_data:protected] =>

Array ( [entity_id] => 1 [entity_type_id] => 1 [attribute_set_id] => 0 [website_id] => 1 [email] => [email protected] [group_id] => 1 [

increment_id] => 000000001 [store_id] => 1 [created_at] => 2007-08-30 23:23:13 [updated_at] => 2008-08-08 12:28:24 [is_active] => 1 [

firstname] => John [lastname] => Doe [password_hash] => 2049484a4020ed15d0e4238db22977d5:eg [prefix] => [middlename] => [suffix] =>

[taxvat] => [default_billing] => 274 [default_shipping] => 274 ) [_hasDataChanges:protected] => [_origData:protected] => Array ( [entity_id] =>

1 [entity_type_id] => 1 [attribute_set_id] => 0 [website_id] => 1 [email] => [email protected] [group_id] => 1 [increment_id] =>

000000001 [store_id] => 1 [created_at] => 2007-08-30 23:23:13 [updated_at] => 2008-08-08 12:28:24 [is_active] => 1 [firstname] => John

[lastname] => Doe [password_hash] => 2049484a4020ed15d0e4238db22977d5:eg [prefix] => [middlename] => [suffix] => [taxvat] => [

default_billing] => 274 [default_shipping] => 274 ) [_idFieldName:protected] => entity_id [_isDeleted:protected] => [_oldFieldsMap:protected] =>

Array ( ) [_syncFieldsMap:protected] => Array ( ) )

Standard practices used in Magento

Structured data output

Page 16: Meet Magento Belarus debug Pavel Novitsky (eng)

Standard practices used in Magento

Useless again?

Page 17: Meet Magento Belarus debug Pavel Novitsky (eng)

Standard practices used in Magento

Varien_Object::getData()

Varien_Object::debug()

Page 18: Meet Magento Belarus debug Pavel Novitsky (eng)

$customer = Mage::getModel('customer/customer')->load(1);

echo '<pre>'.print_r($customer->debug(), true);

Standard practices used in Magento

Structured data output

Page 19: Meet Magento Belarus debug Pavel Novitsky (eng)

[entity_id] => 1

[entity_type_id] => 1

[attribute_set_id] => 0

[website_id] => 1

[email] => [email protected]

[group_id] => 1

[increment_id] => 000000001

[store_id] => 1

[created_at] => 2007-08-30 23:23:13

[updated_at] => 2008-08-08 12:28:24

[is_active] => 1

[firstname] => John

[lastname] => Doe

[password_hash] => 2049484a4020ed15d0e4238db22977d5:eg

[prefix] =>

[middlename] =>

[suffix] =>

[taxvat] =>

[default_billing] => 274

[default_shipping] => 274

Standard practices used in Magento

Structured data output

Page 20: Meet Magento Belarus debug Pavel Novitsky (eng)

Code tracing

debug_backtrace()

print_debug_backtrace()

Standard practices used in Magento

Varien_Debug::backtrace()

Page 21: Meet Magento Belarus debug Pavel Novitsky (eng)

Standard practices used in Magento

Object analysis

get_class()

get_class_vars()

get_declared_classes()

method_exists()

Used everywhere in Magento

Page 22: Meet Magento Belarus debug Pavel Novitsky (eng)

Database queries

echo $sql = "SELECT `some_field1` FROM `table`”;

$res = mysql_query($sql) or die(mysql_error());

Standard practices used in Magento

Page 23: Meet Magento Belarus debug Pavel Novitsky (eng)

Database queries

Display a query:

$myCollection->load(true);

Write a query to the system log:

$myCollection->load(false, true);

Standard practices used in Magento

Page 24: Meet Magento Belarus debug Pavel Novitsky (eng)

Database queries

$model = Mage::getModel('catalog/product')->getCollection();$sql = $model->getSelect()->__toString();echo $sql;

Standard practices used in Magento

Mage::getModel('catalog/product')->getCollection()->load(true);

SELECT `e`.* FROM `catalog_product_entity` AS `e`

Page 25: Meet Magento Belarus debug Pavel Novitsky (eng)

Database queries

Write all the queries to var/debug/pdo_mysql.log

Standard practices used in Magento

lib/Varien/Db/Adapter/Pdo/Mysql.php:

protected $_debug = true; protected $_logAllQueries = true;

Page 26: Meet Magento Belarus debug Pavel Novitsky (eng)

What else?

Page 27: Meet Magento Belarus debug Pavel Novitsky (eng)

Logging

Page 28: Meet Magento Belarus debug Pavel Novitsky (eng)

Let’s experiment

<?php

error_reporting(E_ALL | E_STRICT);ini_set('display_errors', 1);

$mageFilename = 'app/Mage.php';require_once $mageFilename;

Mage::setIsDeveloperMode(true);umask(0);

Mage::app();

// … our code …

Page 29: Meet Magento Belarus debug Pavel Novitsky (eng)

— developer’s Swiss knife

DEBUGGING and PROFILING

Page 30: Meet Magento Belarus debug Pavel Novitsky (eng)

Installation

xdebug.profiler_enable_trigger=onxdebug.remote_autostart=offxdebug.remote_enable=1xdebug.remote_host="127.0.0.1"xdebug.remote_port=9000xdebug.remote_handler="dbgp"xdebug.idekey="netbeans"xdebug.collect_vars=onxdebug.collect_params=4xdebug.show_local_vars=onxdebug.var_display_max_depth=5xdebug.show_exception_trace=on

zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so

; zend_extension_ts="c:\php\ext\php_xdebug-2.0.1-5.2.1.dll"

xDebug

http://xdebug.org/download.php

Setting up

Page 31: Meet Magento Belarus debug Pavel Novitsky (eng)

xDebug

Setting up NetBeans IDE

Page 32: Meet Magento Belarus debug Pavel Novitsky (eng)

xDebug

Well, what good will that do?http://example.com/index.php?XDEBUG_SESSION_START=netbeans

Page 33: Meet Magento Belarus debug Pavel Novitsky (eng)

xDebug

http://example.com/index.php?XDEBUG_SESSION_START=netbeans

easy Xdebug http://bit.ly/LKpvjC

Xdebug helper http://bit.ly/KuCo2c

Page 34: Meet Magento Belarus debug Pavel Novitsky (eng)

Profiling

xdebug.profiler_enable=1

xDebug

Page 35: Meet Magento Belarus debug Pavel Novitsky (eng)

Logs visualization

xDebug — profiling

Webgrind http://bit.ly/LXMGFJ

Kcachegrind http://bit.ly/KGzyAw

WinCacheGrind http://bit.ly/Nh4iPY

Xdebugtoolkit http://bit.ly/LmB4t9

MacCallGrind http://bit.ly/LlerGS

CachegrindVisualizer http://bit.ly/OD6dLy

Page 36: Meet Magento Belarus debug Pavel Novitsky (eng)

The client visited…

Page 37: Meet Magento Belarus debug Pavel Novitsky (eng)

<disable_local_modules>true</disable_local_modules>

Page 38: Meet Magento Belarus debug Pavel Novitsky (eng)

<config><modules>

<Some_Module><active>false</active>

<Some_Module></modules>

</config>

Page 39: Meet Magento Belarus debug Pavel Novitsky (eng)
Page 40: Meet Magento Belarus debug Pavel Novitsky (eng)

Commerce Bug http://bit.ly/M8ggqh

Magento Connect — developer tools

Page 41: Meet Magento Belarus debug Pavel Novitsky (eng)

Developer Toolbar for Magento http://bit.ly/LnSW8s

Magento Connect — developer tools

Page 42: Meet Magento Belarus debug Pavel Novitsky (eng)

Advanced Developer Tools http://bit.ly/Lo1Vqa

Magento Connect — developer tools

Only for versions 1.6.1 and earlier

Page 43: Meet Magento Belarus debug Pavel Novitsky (eng)

Developer Toolbar http://bit.ly/LnD1Hk

Magento Connect — developer tools

Page 44: Meet Magento Belarus debug Pavel Novitsky (eng)

Magento FirePHP http://bit.ly/LnYGyX

Magento Connect — developer tools

Mage::helper('firephp')->send('Lorem ipsum sit amet ..');Mage::helper('firephp')->debug(Mage::getModel('catalog/product')->load(54));

Page 45: Meet Magento Belarus debug Pavel Novitsky (eng)

Magento Connect — developer tools

Developer Helper http://bit.ly/OLauwz

Page 46: Meet Magento Belarus debug Pavel Novitsky (eng)

THANK YOU FOR YOUR ATTENTION

[email protected]