Skip to content

Commit

Permalink
add constants as new actor
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rubio committed Mar 9, 2014
1 parent 71339bb commit 8e09fb7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
84 changes: 84 additions & 0 deletions src/ephp_const.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
-module(ephp_const).
-behaviour(gen_server).
-define(SERVER, ?MODULE).

-compile([warnings_as_errors]).

-include("ephp.hrl").

-record(state, {
const = ?DICT:new() :: dict()
}).

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------

-export([
start_link/0,
get/2,
set/3,
destroy/1
]).

%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------

start_link() ->
gen_server:start_link(?MODULE, [], []).

get(Context, VarPath) ->
gen_server:call(Context, {get, VarPath}).

set(Context, VarPath, Value) ->
gen_server:cast(Context, {set, VarPath, Value}).

destroy(Context) ->
gen_server:cast(Context, stop).

%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------

init([]) ->
{ok, #state{}}.

handle_call({get, Name}, _From, #state{const=Const}=State) ->
{reply, case ?DICT:find(Name, Const) of
{ok, Value} -> Value;
error -> Name
end, State};

handle_call(_Request, _From, State) ->
{reply, ok, State}.

handle_cast({set, Name, Value}, #state{const=Const}=State) ->
NewConst = ?DICT:store(Name, Value, Const),
{noreply, State#state{const=NewConst}};

handle_cast(stop, State) ->
{stop, normal, State};

handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
15 changes: 7 additions & 8 deletions src/ephp_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
funcs :: pid(),
timezone = "Europe/Madrid" :: string(),
output :: pid(),
const = ?DICT:new() :: dict(),
const :: pid(),
global :: pid()
}).

Expand Down Expand Up @@ -125,10 +125,12 @@ init([]) ->
{ok, Funcs} = ephp_func:start_link(),
{ok, Vars} = ephp_vars:start_link(),
{ok, Output} = ephp_output:start_link(),
{ok, Const} = ephp_const:start_link(),
init([#state{
output = Output,
vars = Vars,
funcs = Funcs}]);
funcs = Funcs,
const = Const}]);

init([#state{}=State]) ->
{ok, State}.
Expand Down Expand Up @@ -190,8 +192,8 @@ handle_cast({register, builtin, PHPFunc, Module, Fun}, #state{funcs=Funcs}=State
{noreply, State};

handle_cast({const, Name, Value}, #state{const=Const}=State) ->
NewConst = ?DICT:store(Name, Value, Const),
{noreply, State#state{const=NewConst}};
ephp_const:set(Const, Name, Value),
{noreply, State};

handle_cast(stop, State) ->
{stop, normal, State};
Expand Down Expand Up @@ -442,10 +444,7 @@ resolve({global, GlobalVar}, #state{
{null, State};

resolve(#constant{name=Name}, #state{const=Const}=State) ->
case ?DICT:find(Name, Const) of
{ok, Value} -> {Value, State};
_ -> {Name, State}
end;
{ephp_const:get(Const, Name), State};

resolve(Unknown, #state{}=State) ->
%% TODO: better handle of this errors
Expand Down

0 comments on commit 8e09fb7

Please sign in to comment.