Exemple #1
0
		// Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var phpIncrementrors = Collect(emiter, style, incrementors);
            var phpInitVariables = Collect(emiter, style, initVariables);

            style = style ?? new PhpEmitStyle();

            var header =
                    style.Compression == EmitStyleCompression.Beauty
                    ? "for({0}; {1}; {2})"
                    : "for({0};{1};{2})";
            header = string.Format(header, phpInitVariables, condition.GetPhpCode(style), phpIncrementrors);


            EmitHeaderStatement(emiter, writer, style, header, statement);

            /*
            if (statement == null)
                text += "{}";
            if (style.Compression == EmitStyleCompression.NearCrypto)
                writer.Write(text);
            else
                writer.WriteLn(text);
            if (statement == null) return;
            {
                var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems);
                writer.IncIndent();
                statement.Emit(emiter, writer, iStyle);
                writer.DecIndent();
            }
             */
        }
Exemple #2
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = style ?? new PhpEmitStyle();
            var header = string.Format("while({0})", Condition.GetPhpCode(style));

            EmitHeaderStatement(emiter, writer, style, header, Statement);
        }
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var saveStyleCurrentClass = style.CurrentClass;
            var saveStyleCurrentNamespace = style.CurrentNamespace;
            try
            {
                if (IsEmpty)
                    return;
                if (style.CurrentNamespace == null)
                    style.CurrentNamespace = PhpNamespace.Root;
                if (style.CurrentNamespace != _name.Namespace)
                    throw new Exception("Unable to emit class into different namespace");
                var e = "";
                if (!_baseTypeName.IsEmpty)
                    e = " extends " + _baseTypeName.NameForEmit(style);
                writer.OpenLnF("class {0}{1} {{", Name.ShortName, e);
                style.CurrentClass = _name; // do not move this before "class XXX" is emited
                for (var orderGroup = 0; orderGroup < 3; orderGroup++)
                    foreach (var field in _fields.Where(_ => FieldOrderGroup(_) == orderGroup))
                    {
                        field.Emit(emiter, writer, style);
                    }
                foreach (var me in Methods)
                {
                    me.Emit(emiter, writer, style);
                }
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentClass = saveStyleCurrentClass;
                style.CurrentNamespace = saveStyleCurrentNamespace;
            }
        }
Exemple #4
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var phpIncrementrors = Collect(emiter, style, incrementors);
            var phpInitVariables = Collect(emiter, style, initVariables);

            style = style ?? new PhpEmitStyle();

            var header =
                style.Compression == EmitStyleCompression.Beauty
                    ? "for({0}; {1}; {2})"
                    : "for({0};{1};{2})";

            header = string.Format(header, phpInitVariables, condition.GetPhpCode(style), phpIncrementrors);


            EmitHeaderStatement(emiter, writer, style, header, statement);

            /*
             * if (statement == null)
             *  text += "{}";
             * if (style.Compression == EmitStyleCompression.NearCrypto)
             *  writer.Write(text);
             * else
             *  writer.WriteLn(text);
             * if (statement == null) return;
             * {
             *  var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems);
             *  writer.IncIndent();
             *  statement.Emit(emiter, writer, iStyle);
             *  writer.DecIndent();
             * }
             */
        }
Exemple #5
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpEmitStyle style, string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }

            var writer = new PhpSourceCodeWriter();
            var styleCurrentNamespace = style.CurrentNamespace;

            try
            {
                Emit(emiter, writer, style);

                {
                    var fi = new FileInfo(filename);
                    if (fi.Directory != null)
                    {
                        fi.Directory.Create();
                    }
                    var codeStr = writer.GetCode();
                    var binary  = Encoding.UTF8.GetBytes(codeStr);
                    File.WriteAllBytes(fi.FullName, binary);
                }
            }
            finally
            {
                style.CurrentNamespace = styleCurrentNamespace;
            }
        }
Exemple #6
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = PhpEmitStyle.xClone(style);
            if (!style.AsIncrementor)
            {
                if (_expression is PhpMethodCallExpression)
                {
                    var methodCallExpression = _expression as PhpMethodCallExpression;
                    if (methodCallExpression.CallType == MethodCallStyles.Procedural &&
                        methodCallExpression.Name == "echo")
                    {
                        if (EmitInlineHtml(writer, style))
                        {
                            return;
                        }
                    }
                }
            }
            var code = _expression.GetPhpCode(style);

            if (style.AsIncrementor)
            {
                writer.Write(code);
            }
            else
            {
                writer.WriteLn(code + ";");
            }
        }
Exemple #7
0
        // Private Methods 

        private static void EmitWithNamespace(PhpNamespace ns, PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style, IEnumerable <IEmitable> classesInNamespace)
        {
            if (classesInNamespace == null)
            {
                return;
            }
            var inNamespace = classesInNamespace as IEmitable[] ?? classesInNamespace.ToArray();

            if (!inNamespace.Any())
            {
                return;
            }
            style.CurrentNamespace = ns;
            try
            {
                if (ns.IsRoot)
                {
                    writer.OpenLn("namespace {");
                }
                else
                {
                    writer.OpenLnF("namespace {0} {{", ns.Name.Substring(1));
                }
                foreach (var cl in inNamespace)
                {
                    cl.Emit(emiter, writer, style);
                }
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentNamespace = null;
            }
        }
Exemple #8
0
        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var nsManager = new PhpModuleNamespaceManager();

            style.CurrentNamespace = null;
            if (!string.IsNullOrEmpty(_topComments))
            {
                writer.WriteLn("/*\r\n" + _topComments.Trim() + "\r\n*/");
            }
            var module = this;
            {
                // var noBracketStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.Never);

                {
                    // top code
                    var collectedTopCodeBlock = new PhpCodeBlock();
                    collectedTopCodeBlock.Statements.AddRange(ConvertRequestedToCode());
                    collectedTopCodeBlock.Statements.AddRange(ConvertDefinedConstToCode());
                    if (TopCode != null)
                    {
                        collectedTopCodeBlock.Statements.AddRange(TopCode.Statements);
                    }
                    nsManager.Add(collectedTopCodeBlock.Statements);
                }

                {
                    var classesGbNamespace = module.Classes.GroupBy(u => u.Name.Namespace);
                    foreach (var classesInNamespace in classesGbNamespace.OrderBy(i => !i.Key.IsRoot))
                    {
                        foreach (var c in classesInNamespace)
                        {
                            nsManager.Add(c);
                        }
                    }
                }
                if (BottomCode != null)
                {
                    nsManager.Add(BottomCode.Statements);
                }
                if (!nsManager.Container.Any())
                {
                    return;
                }
                if (nsManager.OnlyOneRootStatement)
                {
                    foreach (var cl in nsManager.Container[0].Items)
                    {
                        cl.Emit(emiter, writer, style);
                    }
                }
                else
                {
                    foreach (var ns in nsManager.Container)
                    {
                        EmitWithNamespace(ns.Name, emiter, writer, style, ns.Items);
                    }
                }
            }
        }
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (_returnValue == null)
                writer.WriteLn("return;");
            else
                writer.WriteLnF("return {0};", _returnValue.GetPhpCode(style));

        }
		// Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var s = style == null ? EmitStyleCompression.Beauty : style.Compression;
            if (s == EmitStyleCompression.NearCrypto)
                writer.Write("continue;");
            else
                writer.WriteLn("continue;");
        }
Exemple #11
0
        // Protected Methods 

        protected void EmitHeaderStatement(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style, string header, IPhpStatement statement)
        {
            style = style ?? new PhpEmitStyle();
            var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems);

            if (style.UseBracketsEvenIfNotNecessary)
            {
                iStyle.Brackets = ShowBracketsEnum.Always;
            }

            var statementToEmit = PhpCodeBlock.Reduce(statement);
            var emptyStatement  = !PhpCodeBlock.HasAny(statementToEmit);


            if (emptyStatement)
            {
                header += "{}";
            }
            if (style.Compression == EmitStyleCompression.NearCrypto)
            {
                writer.Write(header);
            }
            else
            {
                writer.WriteLn(header);
            }
            if (emptyStatement)
            {
                return;
            }


            var myBracket = style.UseBracketsEvenIfNotNecessary;

            if (!myBracket)
            {
                var gf = statementToEmit.GetStatementEmitInfo(iStyle);
                if (gf != StatementEmitInfo.NormalSingleStatement)
                {
                    myBracket = true;
                }
            }
            writer.IncIndent();
            if (myBracket)
            {
                iStyle.Brackets = ShowBracketsEnum.Never;
                writer.OpenLn("{");
            }


            statementToEmit.Emit(emiter, writer, iStyle);
            if (myBracket)
            {
                writer.CloseLn("}");
            }

            writer.DecIndent();
        }
 public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     if (name.IsRoot)
         writer.OpenLn("namespace {");
     else
         writer.OpenLnF("namespace {0} {{", name);
     code.Emit(emiter, writer, style);
     writer.CloseLn("}");
 }
Exemple #13
0
        // Private Methods 

        private static string GetCode(IEmitable method)
        {
            var emiter = new PhpSourceCodeEmiter();
            var writer = new PhpSourceCodeWriter();

            writer.Clear();
            method.Emit(emiter, writer, new PhpEmitStyle());
            return(writer.GetCode(true).Trim());
        }
Exemple #14
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var isBeauty = style == null || style.Compression == EmitStyleCompression.Beauty;

            var ifTrueAny = PhpCodeBlock.HasAny(_ifTrue);
            var ifFalseAny = PhpCodeBlock.HasAny(_ifFalse);
            if (!ifTrueAny && !ifFalseAny) return;

            writer.OpenLnF("if{1}({0}){2}",
                _condition.GetPhpCode(style),
                isBeauty ? " " : "",
                ifTrueAny ? "" : "{}");
            if (ifTrueAny)
            {
                var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems_OR_IfStatement);
                if (style != null && style.UseBracketsEvenIfNotNecessary)
                    iStyle.Brackets = ShowBracketsEnum.Always;
                var bound = PhpCodeBlock.Bound(_ifTrue);
                bound.Emit(emiter, writer, iStyle);
            }
            writer.DecIndent();
            if (!ifFalseAny) return;
            var oneLine = _ifFalse is PhpIfStatement;
            var oldIndent = writer.Intent;
            {
                if (oneLine)
                {
                    writer.Write("else ");
                    writer.SkipIndent = true;
                }
                else
                    writer.OpenLn("else");
                var myBracket = style != null && style.UseBracketsEvenIfNotNecessary;

                var iStyle = PhpEmitStyle.xClone(style,
                    myBracket
                        ? ShowBracketsEnum.Never
                        : ShowBracketsEnum.IfManyItems_OR_IfStatement);

                if (!myBracket)
                {
                    var gf = _ifFalse.GetStatementEmitInfo(iStyle);
                    if (gf != StatementEmitInfo.NormalSingleStatement)
                        myBracket = true;
                }
                if (myBracket)
                {
                    iStyle.Brackets = ShowBracketsEnum.Never;
                    writer.OpenLn("{");
                }                   
                _ifFalse.Emit(emiter, writer, iStyle);
                if (myBracket)
                    writer.CloseLn("}");
            }
            writer.Intent = oldIndent;
        }
        // Public Methods 

        //LangType ItemType, string VarName, IValue Collection, IStatement Statement
        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = style ?? new PhpEmitStyle();
            var arrayOperator = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";
            var header = OneVariable ? "foreach({0} as {3})" : "foreach({0} as {1}{2}{3})";
            header = string.Format(header,
                collection.GetPhpCode(style),
                keyVarname, arrayOperator, valueVarname);
            EmitHeaderStatement(emiter, writer, style, header, statement);
        }
Exemple #16
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (_returnValue == null)
            {
                writer.WriteLn("return;");
            }
            else
            {
                writer.WriteLnF("return {0};", _returnValue.GetPhpCode(style));
            }
        }
Exemple #17
0
        // Public Methods 

        //LangType ItemType, string VarName, IValue Collection, IStatement Statement
        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = style ?? new PhpEmitStyle();
            var arrayOperator = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";
            var header        = OneVariable ? "foreach({0} as {3})" : "foreach({0} as {1}{2}{3})";

            header = string.Format(header,
                                   Collection.GetPhpCode(style),
                                   _keyVarname, arrayOperator, _valueVarname);
            EmitHeaderStatement(emiter, writer, style, header, Statement);
        }
Exemple #18
0
 private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpValue[] collection)
 {
     var list = new List<string>();
     var xStyle = PhpEmitStyle.xClone(style);
     xStyle.AsIncrementor = true;
     foreach (var item in collection)
     {
         list.Add(item.GetPhpCode(xStyle));
     }
     return string.Join(", ", list);
 }
Exemple #19
0
        private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpValue[] collection)
        {
            var list   = new List <string>();
            var xStyle = PhpEmitStyle.xClone(style);

            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                list.Add(item.GetPhpCode(xStyle));
            }
            return(string.Join(", ", list));
        }
Exemple #20
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var s = style == null ? EmitStyleCompression.Beauty : style.Compression;

            if (s == EmitStyleCompression.NearCrypto)
            {
                writer.Write("continue;");
            }
            else
            {
                writer.WriteLn("continue;");
            }
        }
 public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     if (name.IsRoot)
     {
         writer.OpenLn("namespace {");
     }
     else
     {
         writer.OpenLnF("namespace {0} {{", name);
     }
     code.Emit(emiter, writer, style);
     writer.CloseLn("}");
 }
Exemple #22
0
		// Private Methods 

        private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpStatement[] collection)
        {
            var list = new List<string>();
            var xStyle = PhpEmitStyle.xClone(style);
            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                var writer = new PhpSourceCodeWriter();
                writer.Clear();
                item.Emit(emiter, writer, xStyle);
                list.Add(writer.GetCode(true).Trim());
            }
            return string.Join(", ", list);
        }
Exemple #23
0
        // Private Methods 

        private static string Collect(PhpSourceCodeEmiter emiter, PhpEmitStyle style, IPhpStatement[] collection)
        {
            var list   = new List <string>();
            var xStyle = PhpEmitStyle.xClone(style);

            xStyle.AsIncrementor = true;
            foreach (var item in collection)
            {
                var writer = new PhpSourceCodeWriter();
                writer.Clear();
                item.Emit(emiter, writer, xStyle);
                list.Add(writer.GetCode(true).Trim());
            }
            return(string.Join(", ", list));
        }
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            writer.OpenLnF("switch ({0}) {{", expression.GetPhpCode(style));
            foreach (var sec in sections)
            {
                foreach (var l in sec.Labels)
                    writer.WriteLnF("{0}{1}:",
                        l.IsDefault ? "" : "case ",
                        l.IsDefault ? "default" : l.Value.GetPhpCode(style));
                writer.IncIndent();
                sec.Statement.Emit(emiter, writer, style);
                writer.DecIndent();
            }
            writer.CloseLn("}");
        }
Exemple #25
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (statements.Count == 0)
            {
                return;
            }
            var bracketStyle = style == null ? ShowBracketsEnum.IfManyItems : style.Brackets;
            var brack        =
                bracketStyle == ShowBracketsEnum.Never
                ? false
                : bracketStyle == ShowBracketsEnum.Always
                ? true
                : statements == null || !(statements.Count == 1);

            if (statements != null && statements.Count == 1 && bracketStyle == ShowBracketsEnum.IfManyItems_OR_IfStatement)
            {
                if (statements[0] is PhpIfStatement)
                {
                    brack = true;
                }
            }


            var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.Never);

            if (!brack && bracketStyle != ShowBracketsEnum.Never && statements.Count == 1)
            {
                var tmp = statements[0];
                var gf  = tmp.GetStatementEmitInfo(iStyle);
                if (gf != StatementEmitInfo.NormalSingleStatement)
                {
                    brack = true;
                }
            }
            if (brack)
            {
                writer.OpenLn("{");
            }
            foreach (var i in statements)
            {
                i.Emit(emiter, writer, iStyle);
            }
            if (brack)
            {
                writer.CloseLn("}");
            }
            return;
        }
Exemple #26
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var phpIncrementrors = Collect(emiter, style, Incrementors);
            var phpInitVariables = Collect(emiter, style, InitVariables);

            style = style ?? new PhpEmitStyle();

            var header =
                style.Compression == EmitStyleCompression.Beauty
                    ? "for({0}; {1}; {2})"
                    : "for({0};{1};{2})";

            header = string.Format(header, phpInitVariables, Condition.GetPhpCode(style), phpIncrementrors);

            EmitHeaderStatement(emiter, writer, style, header, Statement);
        }
Exemple #27
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            writer.OpenLnF("switch ({0}) {{", expression.GetPhpCode(style));
            foreach (var sec in sections)
            {
                foreach (var l in sec.Labels)
                {
                    writer.WriteLnF("{0}{1}:",
                                    l.IsDefault ? "" : "case ",
                                    l.IsDefault ? "default" : l.Value.GetPhpCode(style));
                }
                writer.IncIndent();
                sec.Statement.Emit(emiter, writer, style);
                writer.DecIndent();
            }
            writer.CloseLn("}");
        }
        public string GetPhpCode(PhpEmitStyle style)
        {
            /*
             echo preg_replace_callback('~-([a-z])~', function ($match) {
    return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
             */
            var s = PhpEmitStyle.xClone(style);
            s.AsIncrementor = true;
            var e = new PhpSourceCodeEmiter();
            var wde = new PhpSourceCodeWriter();
            wde.Clear();
            methodDefinition.Emit(e, wde, s);
            var code = wde.GetCode(true).Trim();
            return code;
        }
Exemple #29
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var saveStyleCurrentClass     = style.CurrentClass;
            var saveStyleCurrentNamespace = style.CurrentNamespace;

            try
            {
                if (IsEmpty)
                {
                    return;
                }
                if (style.CurrentNamespace == null)
                {
                    style.CurrentNamespace = PhpNamespace.Root;
                }
                if (style.CurrentNamespace != Name.Namespace)
                {
                    throw new Exception("Unable to emit class into different namespace");
                }
                var e = "";
                if (!_baseTypeName.IsEmpty)
                {
                    e = " extends " + _baseTypeName.NameForEmit(style);
                }
                writer.OpenLnF("class {0}{1} {{", Name.ShortName, e);
                style.CurrentClass = Name; // do not move this before "class XXX" is emited
                for (var orderGroup = 0; orderGroup < 3; orderGroup++)
                {
                    foreach (var field in Fields.Where(_ => FieldOrderGroup(_) == orderGroup))
                    {
                        field.Emit(emiter, writer, style);
                    }
                }
                foreach (var me in Methods)
                {
                    me.Emit(emiter, writer, style);
                }
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentClass     = saveStyleCurrentClass;
                style.CurrentNamespace = saveStyleCurrentNamespace;
            }
        }
        // Public Methods 
        

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (_isConst)
            {
                //  const CONSTANT = 'constant value';
                writer.WriteLnF("const {0} = {1};", Name, _constValue.GetPhpCode(style));
                return;
            }

            var a = string.Format("{0}{1} ${2}",
                _visibility.ToString().ToLower(),
                _isStatic ? " static" : "",
                Name
                );
            if (_constValue != null)
                a += " = " + _constValue.GetPhpCode(style);
            writer.WriteLn(a + ";");
        }
        // Public Methods 

        public virtual void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter code, PhpEmitStyle style)
        {

            // public function addFunction($function, $namespace = '') 
            var accessModifiers = GetAccessModifiers();
            var param = Arguments == null ? "" : string.Join(", ", Arguments.Select(u => u.GetPhpCode(style)));
            code.OpenLnF("{0} function {1}({2}) {{", accessModifiers, Name, param);
            {
                var g = GetGlobals();
                if (!string.IsNullOrEmpty(g))
                    code.WriteLnF("global {0};", g);
            }
            foreach (var statement in Statements)
            {
                var g = PhpEmitStyle.xClone(style);
                g.Brackets = ShowBracketsEnum.Never;
                statement.Emit(emiter, code, g);
            }
            code.CloseLn("}");
        }
        public string GetPhpCode(PhpEmitStyle style)
        {
            /*
             * echo preg_replace_callback('~-([a-z])~', function ($match) {
             * return strtoupper($match[1]);
             * }, 'hello-world');
             * // outputs helloWorld
             */
            var s = PhpEmitStyle.xClone(style);

            s.AsIncrementor = true;
            var e   = new PhpSourceCodeEmiter();
            var wde = new PhpSourceCodeWriter();

            wde.Clear();
            MethodDefinition.Emit(e, wde, s);
            var code = wde.GetCode(true).Trim();

            return(code);
        }
        // Public Methods 


        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (IsConst)
            {
                //  const CONSTANT = 'constant value';
                writer.WriteLnF("const {0} = {1};", Name, ConstValue.GetPhpCode(style));
                return;
            }

            var a = string.Format("{0}{1} ${2}",
                                  Visibility.ToString().ToLower(),
                                  IsStatic ? " static" : "",
                                  Name
                                  );

            if (ConstValue != null)
            {
                a += " = " + ConstValue.GetPhpCode(style);
            }
            writer.WriteLn(a + ";");
        }
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = PhpEmitStyle.xClone(style);
            if (!style.AsIncrementor)
                if (_expression is PhpMethodCallExpression)
                {
                    var methodCallExpression = _expression as PhpMethodCallExpression;
                    if (methodCallExpression.CallType == MethodCallStyles.Procedural
                        && methodCallExpression.Name == "echo")
                        if (EmitInlineHtml(writer, style))
                            return;
                }
            var code = _expression.GetPhpCode(style);
            if (style.AsIncrementor)
            {
                writer.Write(code);
            }
            else
                writer.WriteLn(code + ";");

        }
        // Public Methods 

        public virtual void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter code, PhpEmitStyle style)
        {
            // public function addFunction($function, $namespace = '')
            var accessModifiers = GetAccessModifiers();
            var param           = Arguments == null ? "" : string.Join(", ", Arguments.Select(u => u.GetPhpCode(style)));

            code.OpenLnF("{0} function {1}({2}) {{", accessModifiers, Name, param);
            {
                var g = GetGlobals();
                if (!string.IsNullOrEmpty(g))
                {
                    code.WriteLnF("global {0};", g);
                }
            }
            foreach (var statement in Statements)
            {
                var g = PhpEmitStyle.xClone(style);
                g.Brackets = ShowBracketsEnum.Never;
                statement.Emit(emiter, code, g);
            }
            code.CloseLn("}");
        }
Exemple #36
0
        // Private Methods 

        private static void EmitWithNamespace(PhpNamespace ns, PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style, IEnumerable<IEmitable> classesInNamespace)
        {
            if (classesInNamespace == null)
                return;
            var inNamespace = classesInNamespace as IEmitable[] ?? classesInNamespace.ToArray();
            if (!inNamespace.Any())
                return;
            style.CurrentNamespace = ns;
            try
            {
                if (ns.IsRoot)
                    writer.OpenLn("namespace {");
                else
                    writer.OpenLnF("namespace {0} {{", ns.Name.Substring(1));
                foreach (var cl in inNamespace)
                    cl.Emit(emiter, writer, style);
                writer.CloseLn("}");
            }
            finally
            {
                style.CurrentNamespace = null;
            }
        }
Exemple #37
0
        // Protected Methods 

        protected override string GetAccessModifiers()
        {
            return(PhpSourceCodeEmiter.GetAccessModifiers(this));
        }
Exemple #38
0
 public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     throw new NotImplementedException();
 }
Exemple #39
0
 public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     writer.WriteLn("break;");
 }
Exemple #40
0
 public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
 {
     writer.WriteLn("break;");
 }
Exemple #41
0
        // Public Methods 

        public abstract void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style);
Exemple #42
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var isBeauty = style == null || style.Compression == EmitStyleCompression.Beauty;

            var ifTrueAny  = PhpCodeBlock.HasAny(_ifTrue);
            var ifFalseAny = PhpCodeBlock.HasAny(_ifFalse);

            if (!ifTrueAny && !ifFalseAny)
            {
                return;
            }

            writer.OpenLnF("if{1}({0}){2}",
                           Condition.GetPhpCode(style),
                           isBeauty ? " " : "",
                           ifTrueAny ? "" : "{}");
            if (ifTrueAny)
            {
                var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.IfManyItems_OR_IfStatement);
                if (style != null && style.UseBracketsEvenIfNotNecessary)
                {
                    iStyle.Brackets = ShowBracketsEnum.Always;
                }
                var bound = PhpCodeBlock.Bound(_ifTrue);
                bound.Emit(emiter, writer, iStyle);
            }

            writer.DecIndent();
            if (!ifFalseAny)
            {
                return;
            }
            var oneLine   = _ifFalse is PhpIfStatement;
            var oldIndent = writer.Intent;

            {
                if (oneLine)
                {
                    writer.Write("else ");
                    writer.SkipIndent = true;
                }
                else
                {
                    writer.OpenLn("else");
                }

                var myBracket = style != null && style.UseBracketsEvenIfNotNecessary;

                var iStyle = PhpEmitStyle.xClone(style,
                                                 myBracket
                        ? ShowBracketsEnum.Never
                        : ShowBracketsEnum.IfManyItems_OR_IfStatement);

                if (!myBracket)
                {
                    var gf = _ifFalse.GetStatementEmitInfo(iStyle);
                    if (gf != StatementEmitInfo.NormalSingleStatement)
                    {
                        myBracket = true;
                    }
                }

                if (myBracket)
                {
                    iStyle.Brackets = ShowBracketsEnum.Never;
                    writer.OpenLn("{");
                }

                _ifFalse.Emit(emiter, writer, iStyle);
                if (myBracket)
                {
                    writer.CloseLn("}");
                }
            }
            writer.Intent = oldIndent;
        }
Exemple #43
0
        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            var nsManager = new PhpModuleNamespaceManager();
            style.CurrentNamespace = null;
            if (!string.IsNullOrEmpty(_topComments))
                writer.WriteLn("/*\r\n" + _topComments.Trim() + "\r\n*/");
            var module = this;
            {
                // var noBracketStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.Never);

                #region Top code

                {
                    // top code
                    var collectedTopCodeBlock = new PhpCodeBlock();
                    collectedTopCodeBlock.Statements.AddRange(ConvertRequestedToCode());
                    collectedTopCodeBlock.Statements.AddRange(ConvertDefinedConstToCode());
                    if (_topCode != null)
                        collectedTopCodeBlock.Statements.AddRange(_topCode.Statements);
                    nsManager.Add(collectedTopCodeBlock.Statements);
                }

                #endregion

                {
                    var classesGbNamespace = module.Classes.GroupBy(u => u.Name.Namespace);
                    foreach (var classesInNamespace in classesGbNamespace.OrderBy(i => !i.Key.IsRoot))
                        foreach (var c in classesInNamespace)
                            nsManager.Add(c);
                }
                if (_bottomCode != null)
                    nsManager.Add(_bottomCode.Statements);
                if (!nsManager.Container.Any()) 
                    return;
                if (nsManager.OnlyOneRootStatement)
                    foreach (var cl in nsManager.Container[0].Items)
                        cl.Emit(emiter, writer, style);
                else
                    foreach (var ns in nsManager.Container)
                        EmitWithNamespace(ns.Name, emiter, writer, style, ns.Items);
            }
        }
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter code, PhpEmitStyle style)
        {
            Refactor();
            base.Emit(emiter, code, style);
        }
Exemple #45
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            style = style ?? new PhpEmitStyle();
            var header = string.Format("while({0})", _condition.GetPhpCode(style));
            EmitHeaderStatement(emiter, writer, style, header, _statement);
        }
Exemple #46
0
        // Public Methods 

        public override void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter code, PhpEmitStyle style)
        {
            Refactor();
            base.Emit(emiter, code, style);
        }
Exemple #47
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpSourceCodeWriter writer, PhpEmitStyle style)
        {
            if (statements.Count == 0)
                return;
            var bracketStyle = style == null ? ShowBracketsEnum.IfManyItems : style.Brackets;
            var brack =
                bracketStyle == ShowBracketsEnum.Never
                ? false
                : bracketStyle == ShowBracketsEnum.Always
                ? true
                : statements == null || !(statements.Count == 1);

            if (statements != null && statements.Count == 1 && bracketStyle == ShowBracketsEnum.IfManyItems_OR_IfStatement)
                if (statements[0] is PhpIfStatement)
                    brack = true;


            var iStyle = PhpEmitStyle.xClone(style, ShowBracketsEnum.Never);
            if (!brack && bracketStyle != ShowBracketsEnum.Never && statements.Count == 1)
            {

                var tmp = statements[0];
                var gf = tmp.GetStatementEmitInfo(iStyle);
                if (gf != StatementEmitInfo.NormalSingleStatement)
                    brack = true;

            }
            if (brack)
                writer.OpenLn("{");
            foreach (var i in statements)
                i.Emit(emiter, writer, iStyle);
            if (brack)
                writer.CloseLn("}");
            return;
        }
Exemple #48
0
        // Public Methods 

        public void Emit(PhpSourceCodeEmiter emiter, PhpEmitStyle style, string filename)
        {

            if (string.IsNullOrEmpty(filename))
                throw new ArgumentNullException("filename");

            var writer = new PhpSourceCodeWriter();
            var styleCurrentNamespace = style.CurrentNamespace;
            try
            {
                Emit(emiter, writer, style);

                #region Save to file
                {
                    var fi = new FileInfo(filename);
                    if (fi.Directory != null) fi.Directory.Create();
                    var codeStr = writer.GetCode();
                    var binary = Encoding.UTF8.GetBytes(codeStr);
                    File.WriteAllBytes(fi.FullName, binary);
                }
                #endregion
            }
            finally
            {
                style.CurrentNamespace = styleCurrentNamespace;
            }
        }