Example #1
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);
                    }
                }
            }
        }
Example #2
0
 public static PhpCodeBlock Bound(IPhpStatement s)
 {
     if (s is PhpCodeBlock)
         return s as PhpCodeBlock;
     var g = new PhpCodeBlock();
     g.statements.Add(s);
     return g;
 }
Example #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();
        }
Example #4
0
        public static PhpCodeBlock Bound(IPhpStatement s)
        {
            if (s is PhpCodeBlock)
            {
                return(s as PhpCodeBlock);
            }
            var g = new PhpCodeBlock();

            g.statements.Add(s);
            return(g);
        }
Example #5
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;
        }
Example #6
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);
            }
        }