forked from pheryjs/phery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhery.php
3359 lines (3076 loc) · 89 KB
/
Phery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The MIT License (MIT)
*
* Copyright © 2010-2013 Paulo Cesar, http://phery-php-ajax.net/
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the “Software”), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @link http://phery-php-ajax.net/
* @author Paulo Cesar
* @version 2.6.0
* @license http://opensource.org/licenses/MIT MIT License
*/
/**
* Main class for Phery.js
*
* @package Phery
*/
class Phery implements ArrayAccess {
/**
* Exception on callback() function
* @see callback()
* @type int
*/
const ERROR_CALLBACK = 0;
/**
* Exception on process() function
* @see process()
*/
const ERROR_PROCESS = 1;
/**
* Exception on set() function
* @see set()
*/
const ERROR_SET = 2;
/**
* Exception when the CSRF is invalid
* @see process()
*/
const ERROR_CSRF = 4;
/**
* Exception on static functions
* @see link_to()
* @see select_for()
* @see form_for()
*/
const ERROR_TO = 3;
/**
* Default encoding for your application
* @var string
*/
public static $encoding = 'UTF-8';
/**
* Expose the paths on PheryResponse exceptions
* @var bool
*/
public static $expose_paths = false;
/**
* The functions registered
* @var array
*/
protected $functions = array();
/**
* The callbacks registered
* @var array
*/
protected $callbacks = array();
/**
* The callback data to be passed to callbacks and responses
* @var array
*/
protected $data = array();
/**
* Static instance for singleton
* @var Phery
* @static
*/
protected static $instance = null;
/**
* Render view function
* @var array
*/
protected $views = array();
/**
* Config
*
* <code>
* 'exit_allowed' (boolean)
* 'exceptions' (boolean)
* 'return' (boolean)
* 'error_reporting' (int)
* 'csrf' (boolean)
* 'set_always_available' (boolean)
* </code>
* @var array
*
* @see config()
*/
protected $config = array();
/**
* If the class was just initiated
* @var bool
*/
private $init = true;
/**
* Construct the new Phery instance
* @param array $config Config array
*/
public function __construct(array $config = array())
{
$this->callbacks = array(
'before' => array(),
'after' => array()
);
$config = array_replace(
array(
'exit_allowed' => true,
'exceptions' => false,
'return' => false,
'csrf' => false,
'set_always_available' => false,
'error_reporting' => false
), $config
);
$this->config($config);
}
/**
* Set callbacks for before and after filters.
* Callbacks are useful for example, if you have 2 or more AJAX functions, and you need to perform
* the same data manipulation, like removing an 'id' from the $_POST['args'], or to check for potential
* CSRF or SQL injection attempts on all the functions, clean data or perform START TRANSACTION for database, etc
*
* @param array $callbacks The callbacks
*
* <pre>
* array(
*
* // Set a function to be called BEFORE
* // processing the request, if it's an
* // AJAX to be processed request, can be
* // an array of callbacks
*
* 'before' => array|function,
*
* // Set a function to be called AFTER
* // processing the request, if it's an AJAX
* // processed request, can be an array of
* // callbacks
*
* 'after' => array|function
* );
* </pre>
*
* The callback function should be
*
* <pre>
*
* // $additional_args is passed using the callback_data() function,
* // in this case, a before callback
*
* function before_callback($ajax_data, $internal_data){
* // Do stuff
* $_POST['args']['id'] = $additional_args['id'];
* return true;
* }
*
* // after callback would be to save the data perhaps? Just to keep the code D.R.Y.
*
* function after_callback($ajax_data, $internal_data, $PheryResponse){
* $this->database->save();
* $PheryResponse->merge(PheryResponse::factory('#loading')->fadeOut());
* return true;
* }
* </pre>
*
* Returning false on the callback will make the process() phase to RETURN, but won't exit.
* You may manually exit on the after callback if desired
* Any data that should be modified will be inside $_POST['args'] (can be accessed freely on 'before',
* will be passed to the AJAX function)
*
* @return Phery
*/
public function callback(array $callbacks)
{
if (isset($callbacks['before']))
{
if (is_array($callbacks['before']) && !is_callable($callbacks['before']))
{
foreach ($callbacks['before'] as $func)
{
if (is_callable($func))
{
$this->callbacks['before'][] = $func;
}
else
{
self::exception($this, "The provided before callback function isn't callable", self::ERROR_CALLBACK);
}
}
}
else
{
if (is_callable($callbacks['before']))
{
$this->callbacks['before'][] = $callbacks['before'];
}
else
{
self::exception($this, "The provided before callback function isn't callable", self::ERROR_CALLBACK);
}
}
}
if (isset($callbacks['after']))
{
if (is_array($callbacks['after']) && !is_callable($callbacks['after']))
{
foreach ($callbacks['after'] as $func)
{
if (is_callable($func))
{
$this->callbacks['after'][] = $func;
}
else
{
self::exception($this, "The provided after callback function isn't callable", self::ERROR_CALLBACK);
}
}
}
else
{
if (is_callable($callbacks['after']))
{
$this->callbacks['after'][] = $callbacks['after'];
}
else
{
self::exception($this, "The provided after callback function isn't callable", self::ERROR_CALLBACK);
}
}
}
return $this;
}
/**
* Throw an exception if enabled
*
* @param Phery $phery Instance
* @param string $exception
* @param integer $code
*
* @throws PheryException
* @return boolean
*/
protected static function exception($phery, $exception, $code)
{
if ($phery instanceof Phery && $phery->config['exceptions'] === true)
{
throw new PheryException($exception, $code);
}
return false;
}
/**
* Set any data to pass to the callbacks
*
* @param mixed $args,... Parameters, can be anything
*
* @return Phery
*/
public function data($args)
{
foreach (func_get_args() as $arg)
{
if (is_array($arg))
{
$this->data = array_merge_recursive($arg, $this->data);
}
else
{
$this->data[] = $arg;
}
}
return $this;
}
/**
* Encode PHP code to put inside data-phery-args, usually for updating the data there
*
* @param array $data Any data that can be converted using json_encode
* @param string $encoding Encoding for the arguments
*
* @return string Return json_encode'd and htmlentities'd string
*/
public static function args(array $data, $encoding = 'UTF-8')
{
return htmlentities(json_encode($data), ENT_COMPAT, $encoding, false);
}
/**
* Output the meta HTML with the token.
* This method needs to use sessions through session_start
*
* @param bool $check Check if the current token is valid
* @param bool $force It will renew the current hash every call
* @return string|bool
*/
public function csrf($check = false, $force = false)
{
if ($this->config['csrf'] !== true)
{
return !empty($check) ? true : '';
}
if (session_id() == '')
{
@session_start();
}
if ($check === false)
{
if ((!empty($_SESSION['phery']['csrf']) && $force) || empty($_SESSION['phery']['csrf']))
{
$token = sha1(uniqid(microtime(true), true));
$_SESSION['phery'] = array(
'csrf' => $token
);
$token = base64_encode($token);
}
else
{
$token = base64_encode($_SESSION['phery']['csrf']);
}
return "<meta id=\"csrf-token\" name=\"csrf-token\" content=\"{$token}\" />\n";
}
else
{
if (empty($_SESSION['phery']['csrf']))
{
return false;
}
return $_SESSION['phery']['csrf'] === base64_decode($check, true);
}
}
/**
* Check if the current call is an ajax call
*
* @param bool $is_phery Check if is an ajax call and a phery specific call
*
* @static
* @return bool
*/
public static function is_ajax($is_phery = false)
{
switch ($is_phery)
{
case true:
return (bool)(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strcasecmp($_SERVER['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest') === 0 &&
strtoupper($_SERVER['REQUEST_METHOD']) === 'POST' &&
!empty($_SERVER['HTTP_X_PHERY']));
case false:
return (bool)(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strcasecmp($_SERVER['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest') === 0);
}
return false;
}
/**
* Strip slashes recursive
*
* @param array|string $variable
* @return array|string
*/
private function stripslashes_recursive($variable)
{
if (!empty($variable) && is_string($variable))
{
return stripslashes($variable);
}
if (!empty($variable) && is_array($variable))
{
foreach ($variable as $i => $value)
{
$variable[$i] = $this->stripslashes_recursive($value);
}
}
return $variable;
}
/**
* Flush loop
*
* @param bool $clean Discard buffers
*/
private static function flush($clean = false)
{
while (ob_get_level() > 0)
{
$clean ? ob_end_clean() : ob_end_flush();
}
}
/**
* Default error handler
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
*/
public static function error_handler($errno, $errstr, $errfile, $errline)
{
self::flush(true);
$response = PheryResponse::factory()->exception($errstr, array(
'code' => $errno,
'file' => Phery::$expose_paths ? $errfile : pathinfo($errfile, PATHINFO_BASENAME),
'line' => $errline
));
self::respond($response);
self::shutdown_handler(false, true);
}
/**
* Default shutdown handler
*
* @param bool $errors
* @param bool $handled
*/
public static function shutdown_handler($errors = false, $handled = false)
{
if ($handled)
{
self::flush();
}
if ($errors === true && ($error = error_get_last()) && !$handled)
{
self::error_handler($error["type"], $error["message"], $error["file"], $error["line"]);
}
if (!$handled)
{
self::flush();
}
if (!session_id())
{
session_write_close();
}
exit;
}
/**
* Helper function to properly output the headers for a PheryResponse in case you need
* to manually return it (like when following a redirect)
*
* @param string|PheryResponse $response The response or a string
* @param bool $echo Echo the response
*
* @return string
*/
public static function respond($response, $echo = true)
{
if ($response instanceof PheryResponse)
{
if (!headers_sent())
{
session_write_close();
header('Cache-Control: no-cache, must-revalidate', true);
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT', true);
header('Content-Type: application/json; charset='.(strtolower(Phery::$encoding)), true);
header('Connection: close', true);
}
}
if ($response)
{
$response = "{$response}";
}
if ($echo === true)
{
echo $response;
}
return $response;
}
/**
* Set the callback for view portions, as defined in Phery.view()
*
* @param array $views Array consisting of array('#id_of_view' => callback)
* The callback is like a normal phery callback, but the second parameter
* receives different data. But it MUST always return a PheryResponse with
* render_view(). You can do any manipulation like you would in regular
* callbacks. If you want to manipulate the DOM AFTER it was rendered, do it
* javascript side, using the afterHtml callback when setting up the views.
*
* <pre>
* Phery::instance()->views(array(
* 'section#container' => function($data, $params){
* return
* PheryResponse::factory()
* ->render_view('html', array('extra data like titles, menus, etc'));
* }
* ));
* </pre>
*
* @return Phery
*/
public function views(array $views)
{
foreach ($views as $container => $callback)
{
if (is_callable($callback))
{
$this->views[$container] = $callback;
}
}
return $this;
}
/**
* Initialize stuff before calling the AJAX function
*
* @return void
*/
protected function before_user_func()
{
if ($this->config['error_reporting'] !== false)
{
set_error_handler('Phery::error_handler', $this->config['error_reporting']);
}
if (empty($_POST['phery']['csrf']))
{
$_POST['phery']['csrf'] = '';
}
if ($this->csrf($_POST['phery']['csrf']) === false)
{
self::exception($this, 'Invalid CSRF token', self::ERROR_CSRF);
}
}
/**
* Process the requests if any
*
* @param boolean $last_call
*
* @return boolean
*/
private function process_data($last_call)
{
$response = null;
$error = null;
$view = false;
if (empty($_POST['phery']))
{
return self::exception($this, 'Non-Phery AJAX request', self::ERROR_PROCESS);
}
if (!empty($_GET['_']))
{
$this->data['requested'] = (int)$_GET['_'];
unset($_GET['_']);
}
if (isset($_GET['_try_count']))
{
$this->data['retries'] = (int)$_GET['_try_count'];
unset($_GET['_try_count']);
}
$args = array();
$remote = false;
if (!empty($_POST['phery']['remote']))
{
$remote = $_POST['phery']['remote'];
}
if (!empty($_POST['phery']['submit_id']))
{
$this->data['submit_id'] = "#{$_POST['phery']['submit_id']}";
}
if ($remote !== false)
{
$this->data['remote'] = $remote;
}
if (!empty($_POST['args']))
{
$args = get_magic_quotes_gpc() ? $this->stripslashes_recursive($_POST['args']) : $_POST['args'];
if ($last_call === true)
{
unset($_POST['args']);
}
}
foreach ($_POST['phery'] as $name => $post)
{
if (!isset($this->data[$name]))
{
$this->data[$name] = $post;
}
}
if (count($this->callbacks['before']))
{
foreach ($this->callbacks['before'] as $func)
{
if (($args = call_user_func($func, $args, $this->data, $this)) === false)
{
return false;
}
}
}
if (!empty($_POST['phery']['view']))
{
$this->data['view'] = $_POST['phery']['view'];
}
if ($remote !== false)
{
if (isset($this->functions[$remote]))
{
if (isset($_POST['phery']['remote']))
{
unset($_POST['phery']['remote']);
}
$this->before_user_func();
$response = call_user_func($this->functions[$remote], $args, $this->data, $this);
foreach ($this->callbacks['after'] as $func)
{
if (call_user_func($func, $args, $this->data, $response, $this) === false)
{
return false;
}
}
if (($response = self::respond($response, false)) === null)
{
$error = 'Response was void for function "'. htmlentities($remote, ENT_COMPAT, null, false). '"';
}
$_POST['phery']['remote'] = $remote;
}
else
{
if ($last_call)
{
self::exception($this, 'The function provided "' . htmlentities($remote, ENT_COMPAT, null, false) . '" isn\'t set', self::ERROR_PROCESS);
}
}
}
else
{
if (!empty($this->data['view']) && isset($this->views[$this->data['view']]))
{
$view = $this->data['view'];
$this->before_user_func();
$response = call_user_func($this->views[$this->data['view']], $args, $this->data, $this);
foreach ($this->callbacks['after'] as $func)
{
if (call_user_func($func, $args, $this->data, $response, $this) === false)
{
return false;
}
}
if (($response = self::respond($response, false)) === null)
{
$error = 'Response was void for view "'. htmlentities($this->data['view'], ENT_COMPAT, null, false) . '"';
}
}
else
{
if ($last_call)
{
if (!empty($this->data['view']))
{
self::exception($this, 'The provided view "' . htmlentities($this->data['view'], ENT_COMPAT, null, false) . '" isn\'t set', self::ERROR_PROCESS);
}
else
{
self::exception($this, 'Empty request', self::ERROR_PROCESS);
}
}
}
}
if ($error !== null)
{
self::error_handler(E_NOTICE, $error, '', 0);
}
elseif ($response === null && $last_call & !$view)
{
$response = PheryResponse::factory();
}
elseif ($response !== null)
{
ob_start();
if (!$this->config['return'])
{
echo $response;
}
}
if (!$this->config['return'] && $this->config['exit_allowed'] === true)
{
if ($last_call || $response !== null)
{
exit;
}
}
elseif ($this->config['return'])
{
self::flush(true);
}
if ($this->config['error_reporting'] !== false)
{
restore_error_handler();
}
return $response;
}
/**
* Process the AJAX requests if any.
*
* @param bool $last_call Set this to false if any other further calls
* to process() will happen, otherwise it will exit
*
* @throws PheryException
* @return boolean Return false if any error happened
*/
public function process($last_call = true)
{
if (self::is_ajax(true))
{
// AJAX call
return $this->process_data($last_call);
}
return true;
}
/**
* Config the current instance of Phery
*
* <code>
* array(
* // Defaults to true, stop further script execution
* 'exit_allowed' => true|false,
*
* // Throw exceptions on errors
* 'exceptions' => true|false,
*
* // Return the responses in the process() call instead of echo'ing
* 'return' => true|false,
*
* // Error reporting temporarily using error_reporting(). 'false' disables
* // the error_reporting and wont try to catch any error.
* // Anything else than false will throw a PheryResponse->exception() with
* // the message
* 'error_reporting' => false|E_ALL|E_DEPRECATED|...
*
* // By default, the function Phery::instance()->set() will only
* // register functions when the current request is an AJAX call,
* // to save resources. In order to use Phery::instance()->get_function()
* // anytime, you need to set this config value to true
* 'set_always_available' => false|true
* );
* </code>
*
* If you pass a string, it will return the current config for the key specified
* Anything else, will output the current config as associative array
*
* @param string|array $config Associative array containing the following options
*
* @return Phery|string|array
*/
public function config($config = null)
{
$register_function = false;
if (!empty($config))
{
if (is_array($config))
{
if (isset($config['exit_allowed']))
{
$this->config['exit_allowed'] = (bool)$config['exit_allowed'];
}
if (isset($config['return']))
{
$this->config['return'] = (bool)$config['return'];
}
if (isset($config['set_always_available']))
{
$this->config['set_always_available'] = (bool)$config['set_always_available'];
}
if (isset($config['exceptions']))
{
$this->config['exceptions'] = (bool)$config['exceptions'];
}
if (isset($config['csrf']))
{
$this->config['csrf'] = (bool)$config['csrf'];
}
if (isset($config['error_reporting']))
{
if ($config['error_reporting'] !== false)
{
$this->config['error_reporting'] = (int)$config['error_reporting'];
}
else
{
$this->config['error_reporting'] = false;
}
$register_function = true;
}
if ($register_function || $this->init)
{
register_shutdown_function('Phery::shutdown_handler', $this->config['error_reporting'] !== false);
$this->init = false;
}
return $this;
}
elseif (!empty($config) && is_string($config) && isset($this->config[$config]))
{
return $this->config[$config];
}
}
return $this->config;
}
/**
* Generates just one instance. Useful to use in many included files. Chainable
*
* @param array $config Associative config array
*
* @see __construct()
* @see config()
* @static
* @return Phery
*/
public static function instance(array $config = array())
{
if (!(self::$instance instanceof Phery))
{
self::$instance = new Phery($config);
}
else if ($config)
{
self::$instance->config($config);
}
return self::$instance;
}
/**
* Sets the functions to respond to the ajax call.
* For security reasons, these functions should not be reacheable through POST/GET requests.
* These will be set only for AJAX requests as it will only be set in case of an ajax request,
* to save resources.
*
* You may set the config option "set_always_available" to true to always register the functions
* regardless of if it's an AJAX function or not going on.
*
* The answer/process function, should have the following structure:
*
* <code>
* function func($ajax_data, $callback_data, $phery){
* $r = new PheryResponse; // or PheryResponse::factory();
*
* // Sometimes the $callback_data will have an item called 'submit_id',
* // is the ID of the calling DOM element.
* // if (isset($callback_data['submit_id'])) { }
* // $phery will be the current phery instance that called this callback
*
* $r->jquery('#id')->animate(...);
* return $r; //Should always return the PheryResponse unless you are dealing with plain text
* }
* </code>
*
* @param array $functions An array of functions to register to the instance.
* <pre>
* array(
* 'function1' => 'function',
* 'function2' => array($this, 'method'),
* 'function3' => 'StaticClass::name',
* 'function4' => array(new ClassName, 'method'),
* 'function5' => function($data){}
* );
* </pre>
* @return Phery
*/
public function set(array $functions)
{
if ($this->config['set_always_available'] === false && !self::is_ajax(true))
{
return $this;
}
if (isset($functions) && is_array($functions))
{
foreach ($functions as $name => $func)
{
if (is_callable($func))
{
$this->functions[$name] = $func;
}
else
{
self::exception($this, 'Provided function "' . $name . '" isnt a valid function or method', self::ERROR_SET);
}
}
}
else
{
self::exception($this, 'Call to "set" must be provided an array', self::ERROR_SET);
}
return $this;
}
/**
* Unset a function previously set with set()
*
* @param string $name Name of the function
* @see set()
* @return Phery
*/
public function unset_function($name)
{
if (isset($this->functions[$name]))