@@ -21,70 +21,70 @@ public function __construct(Application $app)
2121 * CORREÇÃO: Signature correta para middleware do Express-PHP
2222 */
2323 public function handle (Request $ req , Response $ res , callable $ next ): void
24- {
25- try {
26- // Verificar se serviços estão disponíveis
27- if (!$ this ->app ->has ('cycle.orm ' )) {
28- throw new \RuntimeException ('Cycle ORM not properly registered ' );
29- }
24+ {
25+ try {
26+ // Verificar se serviços estão disponíveis
27+ if (!$ this ->app ->has ('cycle.orm ' )) {
28+ throw new \RuntimeException ('Cycle ORM not properly registered ' );
29+ }
3030
31- // Injetar serviços principais
32- $ req ->orm = $ this ->app ->make ('cycle.orm ' );
33- $ req ->em = $ this ->app ->make ('cycle.em ' );
34- $ req ->db = $ this ->app ->make ('cycle.database ' );
31+ // Injetar serviços principais
32+ $ req ->orm = $ this ->app ->make ('cycle.orm ' );
33+ $ req ->em = $ this ->app ->make ('cycle.em ' );
34+ $ req ->db = $ this ->app ->make ('cycle.database ' );
3535
36- // CORREÇÃO: Helper repository mais robusto
37- $ req ->repository = function (string $ entityClass ) use ($ req ) {
38- if (!class_exists ($ entityClass )) {
39- throw new \InvalidArgumentException ("Entity class {$ entityClass } does not exist " );
40- }
41- return $ req ->orm ->getRepository ($ entityClass );
42- };
36+ // CORREÇÃO: Helper repository mais robusto
37+ $ req ->repository = function (string $ entityClass ) use ($ req ) {
38+ if (!class_exists ($ entityClass )) {
39+ throw new \InvalidArgumentException ("Entity class {$ entityClass } does not exist " );
40+ }
41+ return $ req ->orm ->getRepository ($ entityClass );
42+ };
4343
44- // CORREÇÃO: Helper entity com validação aprimorada
45- $ req ->entity = function (string $ entityClass , array $ data = []) {
46- if (!class_exists ($ entityClass )) {
47- throw new \InvalidArgumentException ("Entity class {$ entityClass } does not exist " );
48- }
44+ // CORREÇÃO: Helper entity com validação aprimorada
45+ $ req ->entity = function (string $ entityClass , array $ data = []) {
46+ if (!class_exists ($ entityClass )) {
47+ throw new \InvalidArgumentException ("Entity class {$ entityClass } does not exist " );
48+ }
4949
50- $ entity = new $ entityClass ();
50+ $ entity = new $ entityClass ();
5151
52- // Aplicar dados se fornecidos
53- foreach ($ data as $ property => $ value ) {
54- if (property_exists ($ entity , $ property )) {
55- $ entity ->$ property = $ value ;
52+ // Aplicar dados se fornecidos
53+ foreach ($ data as $ property => $ value ) {
54+ if (property_exists ($ entity , $ property )) {
55+ $ entity ->$ property = $ value ;
56+ }
5657 }
57- }
5858
59- return $ entity ;
60- };
59+ return $ entity ;
60+ };
6161
62- // CORREÇÃO: Helper find com validação
63- $ req ->find = function (string $ entityClass , $ id ) use ($ req ) {
64- return $ req ->repository ($ entityClass )->findByPK ($ id );
65- };
62+ // CORREÇÃO: Helper find com validação
63+ $ req ->find = function (string $ entityClass , $ id ) use ($ req ) {
64+ return $ req ->repository ($ entityClass )->findByPK ($ id );
65+ };
6666
67- // CORREÇÃO: Helper paginate
68- $ req ->paginate = function ($ query , int $ page = 1 , int $ perPage = 15 ) {
69- return \CAFernandes \ExpressPHP \CycleORM \Helpers \CycleHelpers::paginate ($ query , $ page , $ perPage );
70- };
67+ // CORREÇÃO: Helper paginate
68+ $ req ->paginate = function ($ query , int $ page = 1 , int $ perPage = 15 ) {
69+ return \CAFernandes \ExpressPHP \CycleORM \Helpers \CycleHelpers::paginate ($ query , $ page , $ perPage );
70+ };
7171
72- // CORREÇÃO: Helper validateEntity
73- $ req ->validateEntity = function ($ entity ) {
74- $ middleware = new \CAFernandes \ExpressPHP \CycleORM \Middleware \EntityValidationMiddleware ($ this ->app );
75- $ reflection = new \ReflectionMethod ($ middleware , 'validateEntity ' );
76- $ reflection ->setAccessible (true );
77- return $ reflection ->invoke ($ middleware , $ entity );
78- };
72+ // CORREÇÃO: Helper validateEntity
73+ $ req ->validateEntity = function ($ entity ) {
74+ $ middleware = new \CAFernandes \ExpressPHP \CycleORM \Middleware \EntityValidationMiddleware ($ this ->app );
75+ $ reflection = new \ReflectionMethod ($ middleware , 'validateEntity ' );
76+ $ reflection ->setAccessible (true );
77+ return $ reflection ->invoke ($ middleware , $ entity );
78+ };
7979
80- $ next ();
80+ $ next ();
8181
82- } catch (\Exception $ e ) {
83- // Log erro se logger disponível
84- if (method_exists ($ this ->app , 'logger ' )) {
85- $ this ->app ->logger ()->error ('Cycle middleware error: ' . $ e ->getMessage ());
82+ } catch (\Exception $ e ) {
83+ // Log erro se logger disponível
84+ if (method_exists ($ this ->app , 'logger ' )) {
85+ $ this ->app ->logger ()->error ('Cycle middleware error: ' . $ e ->getMessage ());
86+ }
87+ throw $ e ;
8688 }
87- throw $ e ;
8889 }
8990}
90- }
0 commit comments