Ejemplo n.º 1
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            var b = style != null && style.Compression != EmitStyleCompression.Beauty;
            var a = PhpValues.ToPhpCodeValue(Value, b);

            switch (a.Kind)
            {
            case PhpCodeValue.Kinds.Null:
                return("null");

            default:
                return(a.PhpValue);
            }
            //if (_value == null)
            //    return "null";
            //var tt = _value.GetType();
            //if (_value is string)
            //    return PhpStringEmit(_value as string, style);
            //if (_value is double)
            //    return ((double)_value).ToString(System.Globalization.CultureInfo.InvariantCulture);
            //if (_value is bool)
            //    return ((bool)_value) ? "true" : "false";
            //if (tt.IsEnum)
            //{
            //    var tmp = PhpValues.EnumToPhpCode(_value, );
            //    return tmp.Value;

            //}
            //return _value.ToString();
        }
Ejemplo n.º 2
0
 public static string CssBorder(object width, object style, object color)
 {
     width = PhpValues.ToPhpCodeValue(width, true);
     style = PhpValues.ToPhpCodeValue(style, true);
     color = PhpValues.ToPhpCodeValue(color, true);
     return(string.Format("{0} {1} {2}", width, style, color));
 }
Ejemplo n.º 3
0
        public static string TagOpenOpen(object tagname, params object[] atts)
        {
            PhpStringBuilder sb = new PhpStringBuilder();

            sb.Add("<" + PhpValues.ToPhpCodeValue(tagname));
            for (int i = 1; i < atts.Length; i += 2)
            {
                sb.AddFormat(" {0}=\"{1}\"", atts[i - 1], atts[i]);
            }
            // sb.Add(">");
            return(sb.ToString());
        }
Ejemplo n.º 4
0
        public static string Css(params object[] atts)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 1; i < atts.Length; i += 2)
            {
                var k = PhpValues.ToPhpCodeValue(atts[i - 1]);
                var v = PhpValues.ToPhpCodeValue(atts[i]);
                sb.AppendFormat("{0}: {1};", k, v);
            }
            return(sb.ToString());
        }
Ejemplo n.º 5
0
        private EchoEmitItem[] GetEchoItems(PhpEmitStyle style)
        {
            var values = new List <IPhpValue>();

            #region Przygotowanie listy elementów do wyświetlenia
            {
                var methodCall = _expression as PhpMethodCallExpression;
                if (methodCall == null)
                {
                    return
                        (null);
                }
                if (methodCall.CallType != MethodCallStyles.Procedural || methodCall.Name != "echo")
                {
                    return(null);
                }
                foreach (var xx in methodCall.Arguments)
                {
                    values.AddRange(ExpressionSimplifier.ExplodeConcats(xx, "."));
                }
                values = values.Select(AAA).ToList();

                #region Łączenie const string

                for (var i = 1; i < values.Count; i++)
                {
                    var a1 = values[i - 1];
                    var a2 = values[i];
                    if (!(a1 is PhpConstValue) || !(a2 is PhpConstValue))
                    {
                        continue;
                    }
                    var b1 = (a1 as PhpConstValue).Value;
                    var b2 = (a2 as PhpConstValue).Value;
                    var c1 = PhpValues.ToPhpCodeValue(b1);
                    var c2 = PhpValues.ToPhpCodeValue(b2);
                    if (c1.Kind != PhpCodeValue.Kinds.StringConstant || c1.Kind != PhpCodeValue.Kinds.StringConstant)
                    {
                        continue;
                    }
                    values[i - 1] = new PhpConstValue((string)c1.SourceValue + (string)c2.SourceValue);
                    values.RemoveAt(i);
                    i--;
                }

                #endregion
            }
            #endregion
            {
                IPhpValue          echoArguments = null;
                Action <IPhpValue> vv            = u =>
                {
                    // ReSharper disable once AccessToModifiedClosure
                    echoArguments = echoArguments == null ? u : new PhpBinaryOperatorExpression(".", echoArguments, u);
                };

                var result = new List <EchoEmitItem>();
                foreach (var value in values)
                {
                    if (value is PhpConstValue)
                    {
                        var constValue       = (value as PhpConstValue).Value;
                        var constStringValue = constValue as string;
                        if (constStringValue != null)
                        {
                            #region Const-string
                            var lines = SplitToLines(constStringValue);
                            foreach (var i in lines)
                            {
                                if (i.EndsWith("\r\n"))
                                {
                                    vv(new PhpConstValue(i.Substring(0, i.Length - 2)));
                                    vv(new PhpDefinedConstExpression("PHP_EOL", null));
                                    // ReSharper disable once PossibleNullReferenceException
                                    result.Add(new EchoEmitItem(echoArguments.GetPhpCode(style), false));
                                    echoArguments = null;
                                }
                                else
                                {
                                    vv(new PhpConstValue(i));
                                }
                            }
                            continue;
                            #endregion
                        }
                    }
                    vv(value);
                }
                if (echoArguments != null)
                {
                    result.Add(new EchoEmitItem(echoArguments.GetPhpCode(style), false));
                }
                return(result.ToArray());
            }
        }
Ejemplo n.º 6
0
 public static string TagBound(object tagname, object inside, params object[] atts)
 {
     return(TagOpen(tagname, atts) + PhpValues.ToPhpCodeValue(inside) + TagClose(tagname));
 }
Ejemplo n.º 7
0
 public static string TagClose(object tagname)
 {
     return(string.Format("</{0}>", PhpValues.ToPhpCodeValue(tagname)));
 }
Ejemplo n.º 8
0
        protected override IPhpValue VisitPhpBinaryOperatorExpression(PhpBinaryOperatorExpression node)
        {
            switch (node.Operator)
            {
            case ".":
            {
                var _left  = Simplify(node.Left);
                var _right = Simplify(node.Right);
                var n      = new PhpBinaryOperatorExpression(node.Operator, _left, _right);
                var c      = ExplodeConcats(n, ".").ToList();



                for (var i = 1; i < c.Count; i++)
                {
                    var L = c[i - 1];
                    var R = c[i];
                    if (L is PhpConstValue && R is PhpConstValue)
                    {
                        var LValue = (L as PhpConstValue).Value;
                        var RValue = (R as PhpConstValue).Value;
                        if (LValue is string && RValue is string)
                        {
                            c[i - 1] = new PhpConstValue((string)LValue + (string)RValue);
                            c.RemoveAt(i);
                            i--;
                            continue;
                        }
                        var    LCode = PhpValues.ToPhpCodeValue(LValue);
                        var    RCode = PhpValues.ToPhpCodeValue(RValue);
                        string left, right;
                        if (LCode.TryGetPhpString(out left) && RCode.TryGetPhpString(out right))
                        {
                            c[i - 1] = new PhpConstValue(left + right);
                            c.RemoveAt(i);
                            i--;
                            continue;
                        }

                        var msg = string.Format("left={0}, right={1} '{2}+{3}'", LValue, RValue, LValue == null ? null : LValue.GetType().FullName, RValue == null ? null : RValue.GetType().FullName);

#if DEBUG
                        throw new NotImplementedException(msg);
#else
                        Console.WriteLine(msg);
                        Console.WriteLine(L.GetPhpCode(null));
                        Console.WriteLine(R.GetPhpCode(null));
                        continue;
#endif
                    }
                }
                var result = c[0];
                if (c.Count > 1)
                {
                    foreach (var x2 in c.Skip(1))
                    {
                        result = new PhpBinaryOperatorExpression(".", result, x2);
                    }
                }
                return(ReturnSubst(node, result));
            }

            case "|":
            {
                //                    var aLeft = node.Left as PhpConstValue;
                //                    var aRight = node.Right as PhpConstValue;
                //                    if (aLeft != null && aRight != null && aLeft.Value!=null && aRight.Value!=null)
                //                    {
                //                        var typeLeft = aLeft.Value.GetType();
                //                        var typeRight = aRight.Value.GetType();
                //                        if (typeLeft.IsEnum && typeLeft == typeRight)
                //                        {
                //                            var leftValue = (int)aLeft.Value;
                //                            var rightValue = (int)aRight.Value;
                //                            var c = leftValue | rightValue;
                //                            var dd = PhpCodeValue.FromInt(c, true);
                //                            var d = new PhpConstValue(c, false);
                //                            return d;
                //                        }
                //                    }
            }
            break;
            }
            return(node);
        }