Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Filtry i kolejność wykonywania
sf
post
Post #1





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

Ostrzeżenie: (0%)
-----


Posiadam klasę HFilters:
  1. <?php
  2. class HFilters
  3. {
  4. private $aFilterList = array();
  5.  
  6.  
  7. private $oContext;
  8.  
  9.  
  10. public function __construct(& $oContext)
  11. {
  12. $this->oContext = & $oContext;
  13. }
  14.  
  15.  
  16. public function add(iFilter $oFilter, $iPreWeight = 10, $iPostWeight = 10)
  17. {
  18. $this->aFilterList[] = $oFilter;
  19. }
  20.  
  21.  
  22. public function pre()
  23. {
  24. foreach($this->aFilterList as $oFilter) {
  25. $oFilter->pre($this->oContext);
  26. }
  27. }
  28.  
  29.  
  30. public function post()
  31. {
  32. foreach($this->aFilterList as $oFilter) {
  33. $oFilter->post($this->oContext);
  34. }
  35. }
  36. }
  37. ?>


IFilter wygląda następująco:
  1. <?php
  2. interface iFilter
  3. {
  4. public function pre($oContext);
  5.  
  6.  
  7. public function post($oContext);
  8. }
  9. ?>


Mam problem z zaimplementowaniem kolejności wykonywania metod pre() i post() w HFilter wg ustalonwej wagi podanej w metodzie add(). Czy ktoś ma jakiś pomysł?

Ten post edytował sf 7.03.2007, 13:30:49
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Strzałek
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 384
Pomógł: 6
Dołączył: 11.09.2004
Skąd: Grodzisk Mazowiecki

Ostrzeżenie: (0%)
-----


Rozwiązanie takich filtrów jak dla mnie jest trochę mało wygodne. Ja rozwiązuję to o wiele wygodniej i filtry mają większe możliwości. Mianowicie filtr implemetuje interface z jedną metodą - execute która przyjmuje jako argument $filterChain'a z publiczną metodą next() która wykonuje następny filtr. Kod zapisany przed wykonaniem metody next to pre, a po metodzie to post. Proste w działaniu, banalne w implementacji.

  1. <?php
  2.  
  3. interface IFilter {
  4.  public function execute(IFilterChain $filterChain);
  5. }
  6.  
  7. interface IFilterChain {
  8.  public function addFilter(IFilter $filter);
  9.  public function next();
  10. }
  11.  
  12.  class FilterChain implements IFilterChain {
  13.  
  14.  public $filters = array();
  15.  
  16.  
  17. public function addFilter(IFilter $filter){
  18. $this -> filters[] = $filter;
  19. }
  20.  
  21. public function next(){
  22. if($filter = array_shift($this -> filters)){
  23.  $filter -> execute($this);
  24.  }
  25. }
  26.  
  27. public function execute(){
  28.  $this -> next();
  29. }
  30.  
  31.  }
  32.  
  33.  
  34.  class AuthFilter implements IFilter {
  35.  
  36. public function execute(IFilterChain $filterChain){
  37.  
  38. if($auth -> isLoged()){
  39.  $this -> next();
  40. }else{
  41. echo 'zaloguj się';
  42. }
  43.  
  44. }
  45.  
  46.  }
  47.  
  48.  class ExecutionFilter implements IFilter {
  49.  
  50.  public function execute(IFilterChain $filterChain){
  51.  
  52.  
  53. //odpalamy aplikacje
  54.  $filterChain -> next();
  55.  }
  56.  
  57.  }
  58.  
  59.  
  60. $filterChain = new FilterChain();
  61.  
  62. $filterChain -> addFilter(new AuthFilter());
  63. $filterChain -> addFilter(new ExecutionFilter());
  64.  
  65. $filterChain -> execute();
  66.  
  67. ?>


Ufff, pisane z palca na lekcji historii (mamy w sali od inf.) (IMG:http://forum.php.pl/style_emoticons/default/Rkingsmiley.png)

Mam nadzieję że nie ma błędów i ogólny koncept zrozumiany.
Pozdrawiam (IMG:http://forum.php.pl/style_emoticons/default/aarambo.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 17.11.2025 - 19:25