diff --git a/apicast/src/executor.lua b/apicast/src/executor.lua index 300ca6ad9..e3c6540d3 100644 --- a/apicast/src/executor.lua +++ b/apicast/src/executor.lua @@ -15,7 +15,7 @@ local _M = { } local mt = { __index = _M } -- forward all policy methods to the policy chain -for _,phase in policy:phases() do +for _,phase in policy.phases() do _M[phase] = function(self, ...) return self.policy_chain[phase](self.policy_chain, self:context(phase), ...) end diff --git a/apicast/src/policy.lua b/apicast/src/policy.lua index a9d43e7cb..2b2ff3799 100644 --- a/apicast/src/policy.lua +++ b/apicast/src/policy.lua @@ -1,10 +1,10 @@ -local _M = { - PHASES = { - 'init', 'init_worker', - 'rewrite', 'access', 'balancer', - 'header_filter', 'body_filter', - 'post_action', 'log' - } +local _M = { } + +local PHASES = { + 'init', 'init_worker', + 'rewrite', 'access', 'balancer', + 'header_filter', 'body_filter', + 'post_action', 'log' } local setmetatable = setmetatable @@ -28,15 +28,15 @@ function _M.new(name, version) return setmetatable({}, mt) end - for _, phase in _M:phases() do + for _, phase in _M.phases() do policy[phase] = noop end return policy end -function _M:phases() - return ipairs(self.PHASES) +function _M.phases() + return ipairs(PHASES) end return _M diff --git a/apicast/src/policy/local_chain.lua b/apicast/src/policy/local_chain.lua index 17ebd54b0..9c38cae22 100644 --- a/apicast/src/policy/local_chain.lua +++ b/apicast/src/policy/local_chain.lua @@ -16,7 +16,7 @@ local function build_chain(context) end -- forward all policy methods to the policy chain -for _, phase in policy:phases() do +for _, phase in policy.phases() do _M[phase] = function(_, context, ...) local policy_chain = find_policy_chain(context) diff --git a/apicast/src/policy/phase_logger.lua b/apicast/src/policy/phase_logger.lua index c99901fbe..e12c0cd8b 100644 --- a/apicast/src/policy/phase_logger.lua +++ b/apicast/src/policy/phase_logger.lua @@ -5,7 +5,7 @@ local policy = require('policy') local _M = policy.new('Phase logger') -for _, phase in policy:phases() do +for _, phase in policy.phases() do _M[phase] = function() ngx.log(ngx.DEBUG, 'running phase: ', phase) end end diff --git a/apicast/src/policy_chain.lua b/apicast/src/policy_chain.lua index 4df203447..71cb1f0d8 100644 --- a/apicast/src/policy_chain.lua +++ b/apicast/src/policy_chain.lua @@ -82,7 +82,7 @@ local function call_chain(phase_name) end end -for _,phase in policy:phases() do +for _,phase in policy.phases() do _M[phase] = call_chain(phase) end diff --git a/spec/policy_spec.lua b/spec/policy_spec.lua index b8e0a8bc1..cdabc4719 100644 --- a/spec/policy_spec.lua +++ b/spec/policy_spec.lua @@ -31,7 +31,7 @@ describe('policy', function() it('defines a method for each of the nginx phases; they do nothing by default', function() local my_policy = policy.new('custom_authorizer') - for _, phase in policy:phases() do + for _, phase in policy.phases() do -- To be precise, we should check that the function is defined, returns -- nil, and it does not have any side-effects. Checking the latter is -- complicated so we'll leave it for now. @@ -51,7 +51,7 @@ describe('policy', function() describe('.phases', function() it('returns the nginx phases where policies can run, sorted by order of execution', function() local res = {} - for _, phase in policy:phases() do + for _, phase in policy.phases() do table.insert(res, phase) end