예제 #1
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            if (initializers == null || initializers.Length == 0)
            {
                return("array()");
            }
            style = style ?? new PhpEmitStyle();
            var przecinek = style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
            var www       = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";


            var list = new List <string>();

            foreach (var initializeValue in initializers)
            {
                if (initializeValue is PhpAssignExpression)
                {
                    var assignExpression = initializeValue as PhpAssignExpression;
                    if (!string.IsNullOrEmpty(assignExpression.OptionalOperator))
                    {
                        throw new NotSupportedException();
                    }

                    if (assignExpression.Left is PhpArrayAccessExpression)
                    {
                        var left = assignExpression.Left as PhpArrayAccessExpression;
                        if (left.PhpArray is PhpThisExpression)
                        {
                            var o = left.Index + www + assignExpression.Right;
                            list.Add(o);
                            continue;
                        }
                        else
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else if (assignExpression.Left is PhpInstanceFieldAccessExpression)
                    {
                        var l1 = assignExpression.Left as PhpInstanceFieldAccessExpression;
                        var fn = new PhpConstValue(l1.FieldName);
                        var o  = fn.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                    else
                    {
                        var o = assignExpression.Left.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                }
                else
                {
                    list.Add(initializeValue.GetPhpCode(style));
                }
            }
            return("array(" + string.Join(przecinek, list) + ")");
        }
예제 #2
0
 public IPhpValue MakeIncludePath(PhpCodeModuleName relatedTo)
 {
     if (relatedTo.Library == Library)
     {
         var knownPath = ProcessPath(_name + _extension, relatedTo._name + _extension);
         //dirname(__FILE__)
         var __FILE__ = new PhpDefinedConstExpression("__FILE__", null);
         var dirname  = new PhpMethodCallExpression("dirname", __FILE__);
         var path     = new PhpConstValue(PathUtil.MakeUnixPath(PathUtil.UNIX_SEP + knownPath));
         var result   = PhpBinaryOperatorExpression.ConcatStrings(dirname, path);
         return(result);
     }
     else
     {
         string path      = null;
         string pathRelTo = null;
         if (_phpIncludePathExpression is PhpConstValue)
         {
             path = (_phpIncludePathExpression as PhpConstValue).Value as string;
             if (path == null)
             {
                 throw new NotSupportedException();
             }
         }
         else
         {
             return(_phpIncludePathExpression); // assume expression like MPDF_LIB_PATH . 'lib/mpdf/mpdf.php'
         }
         if (relatedTo._phpIncludePathExpression is PhpConstValue)
         {
             pathRelTo = (relatedTo._phpIncludePathExpression as PhpConstValue).Value as string;
             if (pathRelTo == null)
             {
                 throw new NotSupportedException();
             }
         }
         if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(path))
         {
             var knownPath = ProcessPath(path, pathRelTo);
             return(new PhpConstValue(knownPath));
         }
         throw new NotSupportedException();
         //var aaaa = "/" + name + extension;
         //if (phpIncludePathExpression == null)
         //    return null;
         //if (phpIncludePathExpression is PhpConstValue)
         //{
         //    aaaa = ((phpIncludePathExpression as PhpConstValue).Value ?? "").ToString() + aaaa;
         //    return ProcessPath(aaaa, relatedTo.emitPath);
         //    // return new PhpConstValue(aaaa, true);
         //}
         //var a = new PhpConstValue(aaaa, true);
         //var g = new PhpBinaryOperatorExpression(".", phpIncludePathExpression, a);
         //return g;
     }
     throw new NotSupportedException();
 }
예제 #3
0
 public static IPhpValue MakePathValueRelatedToFile(string path)
 {
     path = MakeUnixPath(path + UNIX_SEP);
     if (!path.StartsWith(UNIX_SEP))
         path = UNIX_SEP + path;
     var _FILE_ = new PhpDefinedConstExpression("__FILE__", null);
     var dirinfo = new PhpMethodCallExpression("dirname", _FILE_);
     var a2 = new PhpConstValue(path);
     var concat = new PhpBinaryOperatorExpression(".", dirinfo, a2);
     return concat;
 }
예제 #4
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());
            }
        }
예제 #5
0
        public override string GetPhpCode(PhpEmitStyle style)
        {
            if (initializers == null || initializers.Length == 0)
                return "array()";
            style = style ?? new PhpEmitStyle();
            var przecinek = style.Compression == EmitStyleCompression.Beauty ? ", " : ",";
            var www = style.Compression == EmitStyleCompression.Beauty ? " => " : "=>";


            var list = new List<string>();
            foreach (var initializeValue in initializers)
            {
                if (initializeValue is PhpAssignExpression)
                {
                    var assignExpression = initializeValue as PhpAssignExpression;
                    if (!string.IsNullOrEmpty(assignExpression.OptionalOperator))
                        throw new NotSupportedException();

                    if (assignExpression.Left is PhpArrayAccessExpression)
                    {
                        var left = assignExpression.Left as PhpArrayAccessExpression;
                        if (left.PhpArray is PhpThisExpression)
                        {
                            var o = left.Index + www + assignExpression.Right;
                            list.Add(o);
                            continue;
                        }
                        else
                            throw new NotSupportedException();
                    }
                    else if (assignExpression.Left is PhpInstanceFieldAccessExpression)
                    {
                        var l1 = assignExpression.Left as PhpInstanceFieldAccessExpression;
                        var fn = new PhpConstValue(l1.FieldName);
                        var o = fn.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                    else
                    {
                        var o = assignExpression.Left.GetPhpCode(style) + www + assignExpression.Right;
                        list.Add(o);
                        continue;
                    }
                }
                else
                    list.Add(initializeValue.GetPhpCode(style));
            }
            return "array(" + string.Join(przecinek, list) + ")";
        }
예제 #6
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();
            }
        }