Exemple #1
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 #2
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 #3
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("}");
 }
 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 #6
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 #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 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 #9
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;
        }