Lib_Magento - Version 1.8.0.0

Version Notes

1.8.0.0

Download this release

Release Info

Developer Magento Core Team
Extension Lib_Magento
Version 1.8.0.0
Comparing to
See all releases


Version 1.8.0.0

lib/Magento/Crypt.php ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * {license_notice}
4
+ *
5
+ * @category Magento
6
+ * @package Magento_Crypt
7
+ * @copyright {copyright}
8
+ * @license {license_link}
9
+ */
10
+
11
+ /**
12
+ * Class encapsulates cryptographic algorithm
13
+ */
14
+ class Magento_Crypt
15
+ {
16
+ /**
17
+ * @var string
18
+ */
19
+ protected $_cipher;
20
+
21
+ /**
22
+ * @var string
23
+ */
24
+ protected $_mode;
25
+
26
+ /**
27
+ * @var string
28
+ */
29
+ protected $_initVector;
30
+
31
+ /**
32
+ * Encryption algorithm module handle
33
+ *
34
+ * @var resource
35
+ */
36
+ protected $_handle;
37
+
38
+ /**
39
+ * Constructor
40
+ *
41
+ * @param string $key Secret encryption key.
42
+ * It's unsafe to store encryption key in memory, so no getter for key exists.
43
+ * @param string $cipher Cipher algorithm (one of the MCRYPT_ciphername constants)
44
+ * @param string $mode Mode of cipher algorithm (MCRYPT_MODE_modeabbr constants)
45
+ * @param string|bool $initVector Initial vector to fill algorithm blocks.
46
+ * TRUE generates a random initial vector.
47
+ * FALSE fills initial vector with zero bytes to not use it.
48
+ * @throws Magento_Exception
49
+ */
50
+ public function __construct($key, $cipher = MCRYPT_BLOWFISH, $mode = MCRYPT_MODE_ECB, $initVector = false)
51
+ {
52
+ $this->_cipher = $cipher;
53
+ $this->_mode = $mode;
54
+ $this->_handle = mcrypt_module_open($cipher, '', $mode, '');
55
+ try {
56
+ $maxKeySize = mcrypt_enc_get_key_size($this->_handle);
57
+ if (strlen($key) > $maxKeySize) {
58
+ throw new Magento_Exception('Key must not exceed ' . $maxKeySize . ' bytes.');
59
+ }
60
+ $initVectorSize = mcrypt_enc_get_iv_size($this->_handle);
61
+ if (true === $initVector) {
62
+ /* Generate a random vector from human-readable characters */
63
+ $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
64
+ $initVector = '';
65
+ for ($i = 0; $i < $initVectorSize; $i++) {
66
+ $initVector .= $abc{rand(0, strlen($abc) - 1)};
67
+ }
68
+ } else if (false === $initVector) {
69
+ /* Set vector to zero bytes to not use it */
70
+ $initVector = str_repeat("\0", $initVectorSize);
71
+ } else if (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
72
+ throw new Magento_Exception('Init vector must be a string of ' . $initVectorSize . ' bytes.');
73
+ }
74
+ $this->_initVector = $initVector;
75
+ } catch (Exception $e) {
76
+ mcrypt_module_close($this->_handle);
77
+ throw $e;
78
+ }
79
+ mcrypt_generic_init($this->_handle, $key, $initVector);
80
+ }
81
+
82
+ /**
83
+ * Destructor frees allocated resources
84
+ */
85
+ public function __destruct()
86
+ {
87
+ mcrypt_generic_deinit($this->_handle);
88
+ mcrypt_module_close($this->_handle);
89
+ }
90
+
91
+ /**
92
+ * Retrieve a name of currently used cryptographic algorithm
93
+ *
94
+ * @return string
95
+ */
96
+ public function getCipher()
97
+ {
98
+ return $this->_cipher;
99
+ }
100
+
101
+ /**
102
+ * Mode in which cryptographic algorithm is running
103
+ *
104
+ * @return string
105
+ */
106
+ public function getMode()
107
+ {
108
+ return $this->_mode;
109
+ }
110
+
111
+ /**
112
+ * Retrieve an actual value of initial vector that has been used to initialize a cipher
113
+ *
114
+ * @return string
115
+ */
116
+ public function getInitVector()
117
+ {
118
+ return $this->_initVector;
119
+ }
120
+
121
+ /**
122
+ * Encrypt a data
123
+ *
124
+ * @param string $data String to encrypt
125
+ * @return string
126
+ */
127
+ public function encrypt($data)
128
+ {
129
+ if (strlen($data) == 0) {
130
+ return $data;
131
+ }
132
+ return mcrypt_generic($this->_handle, $data);
133
+ }
134
+
135
+ /**
136
+ * Decrypt a data
137
+ *
138
+ * @param string $data String to decrypt
139
+ * @return string
140
+ */
141
+ public function decrypt($data)
142
+ {
143
+ if (strlen($data) == 0) {
144
+ return $data;
145
+ }
146
+ $data = mdecrypt_generic($this->_handle, $data);
147
+ /*
148
+ * Returned string can in fact be longer than the unencrypted string due to the padding of the data
149
+ * @link http://www.php.net/manual/en/function.mdecrypt-generic.php
150
+ */
151
+ $data = rtrim($data, "\0");
152
+ return $data;
153
+ }
154
+ }
lib/Magento/Db/Adapter/Pdo/Mysql.php ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento PDO MySQL DB adapter
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Magento_Db_Adapter_Pdo_Mysql extends Varien_Db_Adapter_Pdo_Mysql
35
+ {
36
+ /**
37
+ * Returns flag is transaction now?
38
+ *
39
+ * @return bool
40
+ */
41
+ public function isTransaction()
42
+ {
43
+ return (bool)$this->_transactionLevel;
44
+ }
45
+
46
+ /**
47
+ * Batched insert of specified select
48
+ *
49
+ * @param Varien_Db_Select $select
50
+ * @param string $table
51
+ * @param array $fields
52
+ * @param bool $mode
53
+ * @param int $step
54
+ * @return int
55
+ */
56
+ public function insertBatchFromSelect(Varien_Db_Select $select, $table, array $fields = array(),
57
+ $mode = false, $step = 10000
58
+ ) {
59
+ $limitOffset = 0;
60
+ $totalAffectedRows = 0;
61
+
62
+ do {
63
+ $select->limit($step, $limitOffset);
64
+ $result = $this->query(
65
+ $this->insertFromSelect($select, $table, $fields, $mode)
66
+ );
67
+
68
+ $affectedRows = $result->rowCount();
69
+ $totalAffectedRows += $affectedRows;
70
+ $limitOffset += $step;
71
+ } while ($affectedRows > 0);
72
+
73
+ return $totalAffectedRows;
74
+ }
75
+
76
+ /**
77
+ * Retrieve bunch of queries for specified select splitted by specified step
78
+ *
79
+ * @param Varien_Db_Select $select
80
+ * @param string $entityIdField
81
+ * @param int $step
82
+ * @return array
83
+ */
84
+ public function splitSelect(Varien_Db_Select $select, $entityIdField = '*', $step = 10000)
85
+ {
86
+ $countSelect = clone $select;
87
+
88
+ $countSelect->reset(Zend_Db_Select::COLUMNS);
89
+ $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
90
+ $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
91
+ $countSelect->columns('COUNT(' . $entityIdField . ')');
92
+
93
+ $row = $this->fetchRow($countSelect);
94
+ $totalRows = array_shift($row);
95
+
96
+ $bunches = array();
97
+ for ($i = 0; $i <= $totalRows; $i += $step) {
98
+ $bunchSelect = clone $select;
99
+ $bunches[] = $bunchSelect->limit($step, $i);
100
+ }
101
+
102
+ return $bunches;
103
+ }
104
+ }
lib/Magento/Db/Object.php ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Object
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ abstract class Magento_Db_Object
35
+ {
36
+ /**
37
+ * @var Varien_Db_Adapter_Interface
38
+ */
39
+ protected $_adapter = null;
40
+
41
+ /**
42
+ * @var string
43
+ */
44
+ protected $_objectName = null;
45
+
46
+ /**
47
+ * @var string
48
+ */
49
+ protected $_dbType = null;
50
+
51
+ /**
52
+ * @var string
53
+ */
54
+ protected $_schemaName = null;
55
+
56
+ /**
57
+ * Constructor
58
+ * @param $objectName
59
+ * @param Varien_Db_Adapter_Interface $adapter
60
+ * @param $schemaName
61
+ */
62
+ public function __construct(Varien_Db_Adapter_Interface $adapter, $objectName, $schemaName = null)
63
+ {
64
+ $this->_objectName = $objectName;
65
+ $this->_adapter = $adapter;
66
+ $this->_schemaName = $schemaName;
67
+ }
68
+
69
+ /**
70
+ * Returns object type
71
+ *
72
+ * @return string
73
+ */
74
+ public function getDbType()
75
+ {
76
+ return $this->_dbType;
77
+ }
78
+
79
+
80
+ /**
81
+ * Returns current schema name
82
+ *
83
+ * @return string
84
+ */
85
+ protected function _getCurrentSchema()
86
+ {
87
+ return $this->_adapter->fetchOne('SELECT SCHEMA()');
88
+ }
89
+
90
+ /**
91
+ * Returns schema name
92
+ *
93
+ * @return string
94
+ */
95
+ public function getSchemaName()
96
+ {
97
+ if (!$this->_schemaName) {
98
+ $this->_schemaName = $this->_getCurrentSchema();
99
+ }
100
+
101
+ return $this->_schemaName;
102
+ }
103
+
104
+ /**
105
+ * Drop database object
106
+ *
107
+ * @return Magento_Db_Object
108
+ */
109
+ public function drop()
110
+ {
111
+ $query = 'DROP ' . $this->getDbType() . ' IF EXISTS '
112
+ . $this->_adapter->quoteIdentifier($this->_objectName);
113
+ $this->_adapter->query($query);
114
+
115
+ return $this;
116
+ }
117
+
118
+ /**
119
+ * Returns object name
120
+ *
121
+ * @return string
122
+ */
123
+ public function getFullName()
124
+ {
125
+ return ($this->getSchemaName() ? $this->getSchemaName() . '.' : '') . $this->_objectName;
126
+ }
127
+
128
+ /**
129
+ * Returns object name
130
+ *
131
+ * @return string
132
+ */
133
+ public function getObjectName()
134
+ {
135
+ return $this->_objectName;
136
+ }
137
+ }
lib/Magento/Db/Object/Interface.php ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Object_Interface
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ interface Magento_Db_Object_Interface
35
+ {
36
+ /**
37
+ * Describe database object
38
+ *
39
+ * @return array
40
+ */
41
+ public function describe();
42
+
43
+ /**
44
+ * Drop database object
45
+ */
46
+ public function drop();
47
+
48
+ /**
49
+ * Check that database object is exist
50
+ */
51
+ public function isExists();
52
+ }
lib/Magento/Db/Object/Table.php ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Object_Table
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Magento_Db_Object_Table extends Magento_Db_Object implements Magento_Db_Object_Interface
35
+ {
36
+ /**
37
+ * @var string
38
+ */
39
+ protected $_dbType = 'TABLE';
40
+
41
+ /**
42
+ * Check is object exists
43
+ *
44
+ * @return bool
45
+ */
46
+ public function isExists()
47
+ {
48
+ return $this->_adapter->isTableExists($this->_objectName, $this->_schemaName);
49
+ }
50
+
51
+ /**
52
+ * Create a new table from source
53
+ *
54
+ * @param $source Zend_Db_Select
55
+ * @return Magento_Db_Object_Table
56
+ */
57
+ public function createFromSource(Zend_Db_Select $source)
58
+ {
59
+ $this->_adapter->query(
60
+ 'CREATE ' . $this->getDbType() . ' ' . $this->_objectName . ' AS ' . $source
61
+ );
62
+ return $this;
63
+ }
64
+
65
+ /**
66
+ * Describe Table
67
+ *
68
+ * @return array
69
+ */
70
+ public function describe()
71
+ {
72
+ return $this->_adapter->describeTable($this->_objectName, $this->_schemaName);
73
+ }
74
+ }
lib/Magento/Db/Object/Trigger.php ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Object_Trigger
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Magento_Db_Object_Trigger extends Magento_Db_Object implements Magento_Db_Object_Interface
35
+ {
36
+ /**
37
+ * @var string
38
+ */
39
+ protected $_dbType = 'TRIGGER';
40
+
41
+ /**
42
+ * @var array
43
+ */
44
+ protected $_data = array();
45
+
46
+ /**
47
+ * @return bool
48
+ */
49
+ public function isExists()
50
+ {
51
+ if (!isset($this->_data['triggers'][$this->_schemaName])) {
52
+ $this->describe();
53
+ }
54
+
55
+ if (isset($this->_data['triggers'][$this->_schemaName][$this->_objectName])) {
56
+ return true;
57
+ }
58
+
59
+ return false;
60
+ }
61
+
62
+ public function describe()
63
+ {
64
+ $columns = array(
65
+ 'TRIGGER_NAME',
66
+ 'EVENT_MANIPULATION',
67
+ 'EVENT_OBJECT_CATALOG',
68
+ 'EVENT_OBJECT_SCHEMA',
69
+ 'EVENT_OBJECT_TABLE',
70
+ 'ACTION_ORDER',
71
+ 'ACTION_CONDITION',
72
+ 'ACTION_STATEMENT',
73
+ 'ACTION_ORIENTATION',
74
+ 'ACTION_TIMING',
75
+ 'ACTION_REFERENCE_OLD_TABLE',
76
+ 'ACTION_REFERENCE_NEW_TABLE',
77
+ 'ACTION_REFERENCE_OLD_ROW',
78
+ 'ACTION_REFERENCE_NEW_ROW',
79
+ 'CREATED',
80
+ );
81
+ $sql = 'SELECT ' . implode(', ', $columns)
82
+ . ' FROM ' . $this->_adapter->quoteIdentifier(array('INFORMATION_SCHEMA','TRIGGERS'))
83
+ . ' WHERE ';
84
+
85
+ $schema = $this->getSchemaName();
86
+ if ($schema) {
87
+ $sql .= $this->_adapter->quoteIdentifier('EVENT_OBJECT_SCHEMA')
88
+ . ' = ' . $this->_adapter->quote($schema);
89
+ } else {
90
+ $sql .= $this->_adapter->quoteIdentifier('EVENT_OBJECT_SCHEMA')
91
+ . ' != ' . $this->_adapter->quote('INFORMATION_SCHEMA');
92
+ }
93
+
94
+ $results = $this->_adapter->query($sql);
95
+
96
+ $data = array();
97
+ foreach ($results as $row) {
98
+ $row = array_change_key_case($row, CASE_LOWER);
99
+ if (null !== $row['created']) {
100
+ $row['created'] = new DateTime($row['created']);
101
+ }
102
+ $data[$row['trigger_name']] = $row;
103
+ }
104
+ $this->_data['triggers'][$schema] = $data;
105
+
106
+ return $data;
107
+ }
108
+ }
109
+
lib/Magento/Db/Object/View.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Object_View
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Magento_Db_Object_View extends Magento_Db_Object implements Magento_Db_Object_Interface
35
+ {
36
+ const ALGORITHM_MERGE = 'MERGE';
37
+ const ALGORITHM_TEMPTABLE = 'TEMPTABLE';
38
+
39
+ /**
40
+ * @var string
41
+ */
42
+ protected $_dbType = 'VIEW';
43
+
44
+ /**
45
+ * Create view from source
46
+ *
47
+ * @param $source
48
+ * @param string $algorithm
49
+ * @return Magento_Db_Object_View
50
+ */
51
+ public function createFromSource(Zend_Db_Select $source, $algorithm = self::ALGORITHM_MERGE)
52
+ {
53
+ $this->_adapter->query(
54
+ 'CREATE ALGORITHM = ' . $algorithm . ' ' . $this->getDbType() . ' '
55
+ . $this->getObjectName() . ' AS ' . $source
56
+ );
57
+ return $this;
58
+ }
59
+
60
+ /**
61
+ * Describe Table
62
+ *
63
+ * @return array
64
+ */
65
+ public function describe()
66
+ {
67
+ return $this->_adapter->describeTable($this->_objectName, $this->_schemaName);
68
+ }
69
+
70
+ /**
71
+ * Check is object exists
72
+ *
73
+ * @return bool
74
+ */
75
+ public function isExists()
76
+ {
77
+ return $this->_adapter->isTableExists($this->_objectName, $this->_schemaName);
78
+ }
79
+ }
lib/Magento/Db/Sql/Select.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Sql_Select
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Magento_Db_Sql_Select extends Varien_Db_Select
35
+ {
36
+
37
+ }
lib/Magento/Db/Sql/Trigger.php ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Magento
22
+ * @package Magento_Db
23
+ * @copyright Copyright (c) 2012 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Magento_Db_Sql_Trigger
29
+ *
30
+ * @category Magento
31
+ * @package Magento_Db
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Magento_Db_Sql_Trigger
35
+ {
36
+ const NAME = 'name';
37
+ const TARGET = 'target';
38
+ const TIME = 'time';
39
+ const EVENT = 'event';
40
+ const SCOPE = 'scope';
41
+ const BODY = 'body';
42
+
43
+ /**
44
+ * SQL constants
45
+ */
46
+ const SQL_TIME_BEFORE = 'BEFORE';
47
+ const SQL_TIME_AFTER = 'AFTER';
48
+ const SQL_EVENT_INSERT = 'INSERT';
49
+ const SQL_EVENT_UPDATE = 'UPDATE';
50
+ const SQL_EVENT_DELETE = 'DELETE';
51
+ const SQL_FOR_EACH_ROW = 'FOR EACH ROW';
52
+
53
+ /**
54
+ * Trigger parts
55
+ *
56
+ * @var array
57
+ */
58
+ protected $_parts = array();
59
+
60
+ /**
61
+ * Allowed time types
62
+ *
63
+ * @var array
64
+ */
65
+ protected $_timeTypes = array(
66
+ self::SQL_TIME_AFTER,
67
+ self::SQL_TIME_BEFORE
68
+ );
69
+
70
+ /**
71
+ * Allowed event types
72
+ *
73
+ * @var array
74
+ */
75
+ protected $_eventTypes = array(
76
+ self::SQL_EVENT_INSERT,
77
+ self::SQL_EVENT_UPDATE,
78
+ self::SQL_EVENT_DELETE
79
+ );
80
+
81
+ /**
82
+ * Initial trigger structure, for MySQL scope is always "FOR EACH ROW".
83
+ * Time "AFTER" is default
84
+ *
85
+ * @var array
86
+ */
87
+ protected static $_partsInit = array(
88
+ self::TARGET => null,
89
+ self::TIME => self::SQL_TIME_AFTER,
90
+ self::EVENT => null,
91
+ self::SCOPE => self::SQL_FOR_EACH_ROW,
92
+ self::BODY => array()
93
+ );
94
+
95
+ /**
96
+ * Constructor
97
+ */
98
+ public function __construct()
99
+ {
100
+ $this->_parts = self::$_partsInit;
101
+ }
102
+
103
+ /**
104
+ * Validate where all trigger parts set?
105
+ *
106
+ * @return Magento_Db_Sql_Trigger
107
+ * @throws Exception
108
+ */
109
+ protected function _validateIsComplete()
110
+ {
111
+ foreach (array_keys(self::$_partsInit) as $part) {
112
+ if (empty($this->_parts[$part])) {
113
+ throw new Exception('Part [' . $part . '] should be set');
114
+ }
115
+ }
116
+ return $this;
117
+ }
118
+
119
+ /**
120
+ * Set trigger part
121
+ *
122
+ * @param $part
123
+ * @param $value
124
+ * @return Magento_Db_Sql_Trigger
125
+ * @throws InvalidArgumentException
126
+ */
127
+ protected function _setPart($part, $value)
128
+ {
129
+ if ($value != null) {
130
+ $this->_parts[$part] = $value;
131
+ } else {
132
+ throw new InvalidArgumentException('Part [' . $part . '] can not be empty');
133
+ }
134
+ return $this;
135
+ }
136
+
137
+ /**
138
+ * Set trigger part
139
+ *
140
+ * @param $part
141
+ * @return string|array
142
+ * @throws Exception
143
+ */
144
+ protected function _getPart($part)
145
+ {
146
+ if (isset($this->_parts[$part])) {
147
+ return $this->_parts[$part];
148
+ }
149
+
150
+ throw new Exception('Part [' . $part . '] does\'t exists');
151
+ }
152
+
153
+ /**
154
+ * Set body part to trigger
155
+ *
156
+ * @param $part
157
+ * @param $value
158
+ * @return Magento_Db_Sql_Trigger
159
+ * @throws InvalidArgumentException
160
+ */
161
+ public function setBodyPart($part, $value)
162
+ {
163
+ if ($value != null) {
164
+ $this->_parts[self::BODY][$part] = $value;
165
+ } else {
166
+ throw new InvalidArgumentException('Part [' . $part . '] can not be empty');
167
+ }
168
+ return $this;
169
+ }
170
+
171
+
172
+ /**
173
+ * Set body part to trigger
174
+ *
175
+ * @param string $part
176
+ * @return string
177
+ * @throws Exception
178
+ */
179
+ public function getBodyPart($part)
180
+ {
181
+ if (isset($this->_parts[self::BODY][$part])) {
182
+ return $this->_parts[self::BODY][$part];
183
+ }
184
+
185
+ throw new Exception('Part [' . $part . '] does\'t exists');
186
+ }
187
+
188
+ /**
189
+ * Generate trigger name
190
+ *
191
+ * @return string
192
+ */
193
+ protected function _generateTriggerName()
194
+ {
195
+ return strtolower('trg_' . $this->_parts[self::TARGET]
196
+ . '_' . $this->_parts[self::TIME]
197
+ . '_' . $this->_parts[self::EVENT]);
198
+ }
199
+
200
+ /**
201
+ * Set trigger time {BEFORE/AFTER}
202
+ * @param $time
203
+ * @return Magento_Db_Sql_Trigger
204
+ * @throws InvalidArgumentException
205
+ */
206
+ public function setTime($time)
207
+ {
208
+ if (in_array($time, $this->getTimeTypes())) {
209
+ $this->_setPart(self::TIME, $time);
210
+ } else {
211
+ throw new InvalidArgumentException('Unsupported time type!');
212
+ }
213
+ return $this;
214
+ }
215
+
216
+ /**
217
+ * Set trigger event {INSERT/UPDATE/DELETE}
218
+ *
219
+ * @param $event
220
+ * @return Magento_Db_Sql_Trigger
221
+ * @throws InvalidArgumentException
222
+ */
223
+ public function setEvent($event)
224
+ {
225
+ if (in_array($event, $this->getEventTypes())) {
226
+ $this->_setPart(self::EVENT, $event);
227
+ } else {
228
+ throw new InvalidArgumentException('Unsupported event type!');
229
+ }
230
+ return $this;
231
+ }
232
+
233
+ /**
234
+ * Set trigger target, table name
235
+ *
236
+ * @param $target
237
+ * @return Magento_Db_Sql_Trigger
238
+ */
239
+ public function setTarget($target)
240
+ {
241
+ $this->_setPart(self::TARGET, $target);
242
+ return $this;
243
+ }
244
+
245
+ /**
246
+ * Set trigger name
247
+ *
248
+ * @param $name
249
+ * @return Magento_Db_Sql_Trigger
250
+ */
251
+ public function setName($name)
252
+ {
253
+ $this->_setPart(self::NAME, $name);
254
+ return $this;
255
+ }
256
+
257
+ /**
258
+ * Retrieve trigger name.
259
+ * If trigger name does not exists generate it by template 'trg_{TARGET}_{TIME}_{EVENT}'.
260
+ *
261
+ * @return mixed
262
+ */
263
+ public function getName()
264
+ {
265
+ if (empty($this->_parts[self::NAME])) {
266
+ $this->_parts[self::NAME] = $this->_generateTriggerName();
267
+ }
268
+ return $this->_parts[self::NAME];
269
+ }
270
+
271
+ /**
272
+ * Set trigger body
273
+ *
274
+ * @param array|string $body
275
+ * @return Magento_Db_Sql_Trigger
276
+ */
277
+ public function setBody($body)
278
+ {
279
+ if (!is_array($body)) {
280
+ $body = array($body);
281
+ }
282
+ $this->_setPart(self::BODY, $body);
283
+ return $this;
284
+ }
285
+
286
+ /**
287
+ * Get body parts of trigger
288
+ *
289
+ * @return array
290
+ */
291
+ public function getBody()
292
+ {
293
+ return $this->_getPart(self::BODY);
294
+ }
295
+
296
+ /**
297
+ * Get trigger creating SQL script
298
+ *
299
+ * @return string
300
+ */
301
+ public function assemble()
302
+ {
303
+ $this->_validateIsComplete();
304
+ return "CREATE TRIGGER "
305
+ . $this->getName() . "\n"
306
+ . $this->_parts[self::TIME] . " " . $this->_parts[self::EVENT] . "\n"
307
+ . "ON " . $this->_parts[self::TARGET] . " " . $this->_parts[self::SCOPE] . "\n"
308
+ . "BEGIN\n"
309
+ . implode("\n", $this->_parts[self::BODY]) . "\n"
310
+ . "END;\n";
311
+ }
312
+
313
+ /**
314
+ * Implement magic method
315
+ *
316
+ * @return string
317
+ */
318
+ public function __toString()
319
+ {
320
+ return $this->assemble();
321
+ }
322
+
323
+ /**
324
+ * Retrieve list of allowed events
325
+ *
326
+ * @return array
327
+ */
328
+ public function getEventTypes()
329
+ {
330
+ return $this->_eventTypes;
331
+ }
332
+
333
+ /**
334
+ * Retrieve list of allowed time types
335
+ *
336
+ * @return array
337
+ */
338
+ public function getTimeTypes()
339
+ {
340
+ return $this->_timeTypes;
341
+ }
342
+
343
+ /**
344
+ * Reset trigger parts
345
+ *
346
+ * @return Magento_Db_Sql_Trigger
347
+ */
348
+ public function reset()
349
+ {
350
+ $this->_parts = self::$_partsInit;
351
+ return $this;
352
+ }
353
+ }
lib/Magento/Exception.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * {license_notice}
4
+ *
5
+ * @category Magento
6
+ * @package Magento_Exception
7
+ * @copyright {copyright}
8
+ * @license {license_link}
9
+ */
10
+
11
+ class Magento_Exception extends Exception
12
+ {
13
+
14
+ }
lib/Magento/Profiler.php ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * {license_notice}
4
+ *
5
+ * @category Magento
6
+ * @package Magento_Profiler
7
+ * @copyright {copyright}
8
+ * @license {license_link}
9
+ */
10
+
11
+ /**
12
+ * Static class that represents profiling tool
13
+ */
14
+ class Magento_Profiler
15
+ {
16
+ /**
17
+ * Separator literal to assemble timer identifier from timer names
18
+ */
19
+ const NESTING_SEPARATOR = '->';
20
+
21
+ /**
22
+ * FETCH_* constants represent keys to retrieve profiling results
23
+ */
24
+ const FETCH_TIME = 'sum';
25
+ const FETCH_COUNT = 'count';
26
+ const FETCH_AVG = 'avg';
27
+ const FETCH_REALMEM = 'realmem';
28
+ const FETCH_EMALLOC = 'emalloc';
29
+
30
+ /**
31
+ * Storage for timers statistics
32
+ *
33
+ * @var array
34
+ */
35
+ static private $_timers = array();
36
+
37
+ /**
38
+ * Whether profiling is active or not
39
+ *
40
+ * @var bool
41
+ */
42
+ static private $_enabled = false;
43
+
44
+ /**
45
+ * Nesting path that represents namespace to resolve timer names
46
+ *
47
+ * @var array
48
+ */
49
+ static private $_currentPath = array();
50
+
51
+ /**
52
+ * Collection of profiler outputs
53
+ *
54
+ * @var array
55
+ */
56
+ static private $_outputs = array();
57
+
58
+ /**
59
+ * Whether an initialization is done or not
60
+ *
61
+ * @var bool
62
+ */
63
+ static private $_isInitialized = false;
64
+
65
+ /**
66
+ * Supported timer statistics keys
67
+ *
68
+ * @var array
69
+ */
70
+ private static $_supportedFetchKeys = array(
71
+ self::FETCH_TIME,
72
+ self::FETCH_AVG,
73
+ self::FETCH_COUNT,
74
+ self::FETCH_EMALLOC,
75
+ self::FETCH_REALMEM,
76
+ );
77
+
78
+ /**
79
+ * Retrieve unique identifier among all timers
80
+ *
81
+ * @param string|null $timerName Timer name
82
+ * @return string
83
+ */
84
+ private static function _getTimerId($timerName = null)
85
+ {
86
+ $currentPath = self::$_currentPath;
87
+ if ($timerName) {
88
+ $currentPath[] = $timerName;
89
+ }
90
+ return implode(self::NESTING_SEPARATOR, $currentPath);
91
+ }
92
+
93
+ /**
94
+ * Enable profiling.
95
+ * Any call to profiler does nothing until profiler is enabled.
96
+ */
97
+ public static function enable()
98
+ {
99
+ if (!self::$_isInitialized) {
100
+ register_shutdown_function(array(__CLASS__, 'display'));
101
+ self::$_isInitialized = true;
102
+ }
103
+ self::$_enabled = true;
104
+ }
105
+
106
+ /**
107
+ * Disable profiling.
108
+ * Any call to profiler is silently ignored while profiler is disabled.
109
+ */
110
+ public static function disable()
111
+ {
112
+ self::$_enabled = false;
113
+ }
114
+
115
+ /**
116
+ * Reset collected statistics for specified timer or for whole profiler if timer name is omitted
117
+ *
118
+ * @param string|null $timerName
119
+ */
120
+ public static function reset($timerName = null)
121
+ {
122
+ if ($timerName === null) {
123
+ self::$_timers = array();
124
+ self::$_currentPath = array();
125
+ return;
126
+ }
127
+ $timerId = self::_getTimerId($timerName);
128
+ self::$_timers[$timerId] = array(
129
+ 'start' => false,
130
+ self::FETCH_TIME => 0,
131
+ self::FETCH_COUNT => 0,
132
+ self::FETCH_REALMEM => 0,
133
+ self::FETCH_EMALLOC => 0,
134
+ );
135
+ }
136
+
137
+ /**
138
+ * Start collecting statistics for specified timer
139
+ *
140
+ * @param string $timerName
141
+ * @throws Varien_Exception
142
+ */
143
+ public static function start($timerName)
144
+ {
145
+ if (!self::$_enabled) {
146
+ return;
147
+ }
148
+
149
+ if (strpos($timerName, self::NESTING_SEPARATOR) !== false) {
150
+ throw new Varien_Exception('Timer name must not contain a nesting separator.');
151
+ }
152
+
153
+ $timerId = self::_getTimerId($timerName);
154
+
155
+ if (empty(self::$_timers[$timerId])) {
156
+ self::reset($timerName);
157
+ }
158
+
159
+ /* Continue collecting timers statistics under the latest started one */
160
+ self::$_currentPath[] = $timerName;
161
+
162
+ self::$_timers[$timerId]['realmem_start'] = memory_get_usage(true);
163
+ self::$_timers[$timerId]['emalloc_start'] = memory_get_usage();
164
+ self::$_timers[$timerId]['start'] = microtime(true);
165
+ self::$_timers[$timerId][self::FETCH_COUNT]++;
166
+ }
167
+
168
+ /**
169
+ * Stop recording statistics for specified timer.
170
+ * Call with no arguments to stop the recently started timer.
171
+ * Only the latest started timer can be stopped.
172
+ *
173
+ * @param string|null $timerName
174
+ * @throws Varien_Exception
175
+ */
176
+ public static function stop($timerName = null)
177
+ {
178
+ if (!self::$_enabled) {
179
+ return;
180
+ }
181
+
182
+ /* Get current time as quick as possible to make more accurate calculations */
183
+ $time = microtime(true);
184
+
185
+ $latestTimerName = end(self::$_currentPath);
186
+ if ($timerName !== null && $timerName !== $latestTimerName) {
187
+ if (in_array($timerName, self::$_currentPath)) {
188
+ $exceptionMsg = sprintf('Timer "%s" should be stopped before "%s".', $latestTimerName, $timerName);
189
+ } else {
190
+ $exceptionMsg = sprintf('Timer "%s" has not been started.', $timerName);
191
+ }
192
+ throw new Varien_Exception($exceptionMsg);
193
+ }
194
+
195
+ $timerId = self::_getTimerId();
196
+
197
+ self::$_timers[$timerId][self::FETCH_TIME] += ($time - self::$_timers[$timerId]['start']);
198
+ self::$_timers[$timerId]['start'] = false;
199
+ self::$_timers[$timerId][self::FETCH_REALMEM] += memory_get_usage(true);
200
+ self::$_timers[$timerId][self::FETCH_REALMEM] -= self::$_timers[$timerId]['realmem_start'];
201
+ self::$_timers[$timerId][self::FETCH_EMALLOC] += memory_get_usage();
202
+ self::$_timers[$timerId][self::FETCH_EMALLOC] -= self::$_timers[$timerId]['emalloc_start'];
203
+
204
+ /* Move one level up in timers nesting tree */
205
+ array_pop(self::$_currentPath);
206
+ }
207
+
208
+ /**
209
+ * Retrieve statistics on specified timer
210
+ *
211
+ * @param string $timerId
212
+ * @param string $key Information to return
213
+ * @return int|float
214
+ * @throws Varien_Exception
215
+ */
216
+ public static function fetch($timerId, $key = self::FETCH_TIME)
217
+ {
218
+ if (empty(self::$_timers[$timerId])) {
219
+ throw new Varien_Exception(sprintf('Timer "%s" does not exist.', $timerId));
220
+ }
221
+ if (!in_array($key, self::$_supportedFetchKeys)) {
222
+ throw new Varien_Exception(sprintf('Requested key "%s" is not supported.', $key));
223
+ }
224
+ /* FETCH_AVG = FETCH_TIME / FETCH_COUNT */
225
+ $isAvg = ($key == self::FETCH_AVG);
226
+ if ($isAvg) {
227
+ $key = self::FETCH_TIME;
228
+ }
229
+ $result = self::$_timers[$timerId][$key];
230
+ if ($key == self::FETCH_TIME && self::$_timers[$timerId]['start'] !== false) {
231
+ $result += (microtime(true) - self::$_timers[$timerId]['start']);
232
+ }
233
+ if ($isAvg) {
234
+ $result /= self::$_timers[$timerId][self::FETCH_COUNT];
235
+ }
236
+ return $result;
237
+ }
238
+
239
+ /**
240
+ * Retrieve the list of unique timer identifiers
241
+ *
242
+ * @return array
243
+ */
244
+ public static function getTimers()
245
+ {
246
+ return array_keys(self::$_timers);
247
+ }
248
+
249
+ /**
250
+ * Register profiler output instance to display profiling result at the end of execution
251
+ *
252
+ * @param Magento_Profiler_OutputAbstract $output
253