1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
#pragma once namespace prog { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::TextBox^ textBox2; private: System::Windows::Forms::TextBox^ textBox3; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->textBox3 = (gcnew System::Windows::Forms::TextBox()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit(); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(47, 22); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 0; this->button1->Text = L"button1"; this->button1->UseVisualStyleBackColor = true; // // pictureBox1 // this->pictureBox1->Location = System::Drawing::Point(750, 12); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(313, 274); this->pictureBox1->TabIndex = 1; this->pictureBox1->TabStop = false; // // textBox1 // this->textBox1->Location = System::Drawing::Point(47, 64); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(100, 20); this->textBox1->TabIndex = 2; this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged); // // textBox2 // this->textBox2->Location = System::Drawing::Point(47, 90); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(100, 20); this->textBox2->TabIndex = 3; // // textBox3 // this->textBox3->Location = System::Drawing::Point(47, 116); this->textBox3->Name = L"textBox3"; this->textBox3->Size = System::Drawing::Size(100, 20); this->textBox3->TabIndex = 4; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(1075, 364); this->Controls->Add(this->textBox3); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->Controls->Add(this->pictureBox1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit(); this->ResumeLayout(false); this->PerformLayout(); } int x1,y1,x2,y2,a,b,dg, #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { a = gcnew array<float>(dg->RowCount); b = gcnew array<float>(dg->RowCount); Graphics^ gr=pictureBox1->CreateGraphics(); Pen^ p=gcnew Pen(pictureBox1->BackColor); SolidBrush^ sb=gcnew SolidBrush(pictureBox1->BackColor); gr->FillRectangle(sb,0,0,pictureBox1->Width,pictureBox1->Height); a[0]=Convert::ToDouble(dg->Rows[0]->Cells[0]->Value)+pictureBox1->Width/2; b[0]=Convert::ToDouble(dg->Rows[0]->Cells[1]->Value)+pictureBox1->Height/2; for(int i=0;i<dg->RowCount-1;i++) { x1=Convert::ToDouble(dg->Rows[i]->Cells[0]->Value); y1=Convert::ToDouble(dg->Rows[i]->Cells[1]->Value); x2=Convert::ToDouble(dg->Rows[i+1]->Cells[0]->Value); y2=Convert::ToDouble(dg->Rows[i+1]->Cells[1]->Value); x1=x1+pictureBox1->Width/2; y1=y1+pictureBox1->Height/2; x2=x2+pictureBox1->Width/2; y2=y2+pictureBox1->Height/2; gr->DrawLine(Pens::Black,x1,y1,x2,y2); a[i+1]=x2; b[i+1]=y2; //gr->DrawLine(Pens::Black,Convert::ToDouble(dg->Rows[i]->Cells[0]->Value),Convert::ToDouble(dg->Rows[i]->Cells[1]->Value),Convert::ToDouble(dg->Rows[i+1]->Cells[0]->Value),Convert::ToDouble(dg->Rows[i+1]->Cells[1]->Value)); //gr->DrawLine(Pens::Black,Convert::ToInt32(dg->Rows[i]->Cells[0]->Value),Convert::ToInt32(dg->Rows[i]->Cells[1]->Value),Convert::ToInt32(dg->Rows[i+1]->Cells[0]->Value),Convert::ToInt32(dg->Rows[i+1]->Cells[1]->Value)); } }; |
Hi guys haven done stuff on PHP for quite a while, but I was trying to get an coreDB.php file to have DB information and to be open and close from this file.
Everything is working, but when I try to include this on another file, I allways get the error that I put on the title.
coreDB file:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
class ConnectionDB
{
private $dbhost = "localhost";
private $dbuser = "user";
private $dbpass = "password";
private $dbname = "dbname";
public $conn;
public function openDbConnection()
{
try
{
$this->conn = new PDO("mysql:host=$this->dbhost;dbname=$this->dbname", $this->dbuser, $this->dbpass);
echo "Connection Done<br>";
}
catch(PDOException $e)
{
//var_dump($this->conn);
//echo $e->getMessage();
}
}
public function closeDbConnection()
{
try
{
$this->conn = NULL;
echo "Connection Over";
}
catch(PDOException $e)
{
//var_dump($this->conn);
//echo $e->getMessage();
}
}
}?>
The other file, clientNewRecord.php, where the previous is include:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include("coreDB.php");
echo "this is a test";
private $dbConnect = new ConnectionDB();
$this->dbConnect->openDbConnection();
$this->dbConnect->closeDbConnection();?>
I allways get this error: PHP Parse error: syntax error, unexpected ‘private’ when trying to open the clientNewRecord.php
Isn’t when I try to run the $this->dbConnect->openDbConnection(), to go into that file and run from that file?
I am Writing my first code to add and fetch some data from a database .:
Following is the code that I wrote in the Mainactivity.java file and its giving some errors
Such as Syntax error on token «Private», private expected also for «Public»;
I have attached the screen shots as well can someone help regarding this .Because I am new to java and android codes
package com.example.ghrixdatabse;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.TableLayout;
public class MainActivity extends Activity {
private static final EditText FindViewById = null;
String fname,lname,email;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("Mydb1", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);" );
}
Private void Adddata(View view)
{
EditText edittext1=(EditText)findViewById(R.id.firstname);
EditText edittext2=(EditText)findViewById(R.id.lastname);
EditText edittext3=(EditText)findViewById(R.id.email);
fname= edittext1.getText().toString();
lname= edittext2.getText().toString();
email= edittext3.getText().toString();
db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"');");
}
Private void Showdata(View view)
{
Cursor c = db.rawQuery(SELECT * FROM from",null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext () );
tableLayout.setVerticalScrollBarEnabled(true);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("Firstname");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("LastName");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("Email");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableLayout.addView(tableRow);
for (Integer j=0;j < count;j++)
{
tableRow =new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColoumIndex('fname')));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColoumIndex('lanme')));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColoumIndex('email')));
textView1.setPadding(20,20,20,20);
textView2.setPadding(20,20,20,20);
textView3.setPadding(20,20,20,20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tablelayout.addView(tableRow);
c.moveToNext();
}
setContentView(tableLayout);
db.close();
}
Public void close (View view)
{
System.exit(0);
}
}
HERE is the XML FILE :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="49dp"
android:text="FirstName" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:text="Lastname" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_centerVertical="true"
android:text="E-mail" />
<EditText
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/lastname"
android:ems="10" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/email"
android:layout_marginBottom="24dp"
android:onClick="showdata"
android:text="Show Table" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignRight="@+id/button3"
android:layout_marginBottom="26dp"
android:layout_marginRight="17dp"
android:onClick="close"
android:text="Close" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/textView3"
android:onClick="Adddata"
android:text="Create" />
</RelativeLayout>
15 Years Ago
Hello everyone,
I paid for this script which is a community that offers its members the chance to get a club and joijn other members clubs!
However, when people try to join a club they getb this error:
Parse error: syntax error, unexpected T_PRIVATE, expecting ']' in /home/cyberpet/public_html/club_join.pro.php on line 14
I have emailed the guy who owns the website and script who has not bothered replying to the many problems I have encountered so far. Luckily I have been able to sort through the other problems and have had to learn the script as I do not know PHP, MySQL etc.
The line of the script that the above error refers to is as follows:
<?php
/*
Join club (club_join.pro.php)
*/
ob_start();
$rank_check = 1;
include "global.inc.php";
$clubrank_check = 0;
include "club.inc.php";
if ($getclub[private]== 1)
{
die(header(error("club.php?game=$game&clubid=$clubid","You can't join a private club.")));
}
kind regards
justin
Edited
10 Years Ago
by mike_2000_17 because:
Fixed formatting
Hello,
I am getting the following error after updating a code on a Dev instance on Acquia Cloud:
Error: syntax error, unexpected ‘private’ (T_PRIVATE) IN /mnt/gfs/ourfolder/livedev/vendor/symfony/dependency-injection/Compiler/AutowirePass.php, line 178
I am unaware of any changes that have been made by us on that file.
Do you know what might be causing this?
Here is the code for the file, with line 1789 in bold.
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyComponentDependencyInjectionCompiler;
use SymfonyComponentConfigResourceClassExistenceResource;
use SymfonyComponentDependencyInjectionConfigAutowireServiceResource;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentDependencyInjectionDefinition;
use SymfonyComponentDependencyInjectionExceptionAutowiringFailedException;
use SymfonyComponentDependencyInjectionExceptionRuntimeException;
use SymfonyComponentDependencyInjectionLazyProxyProxyHelper;
use SymfonyComponentDependencyInjectionTypedReference;
/**
* Inspects existing service definitions and wires the autowired ones using the type hints of their classes.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
class AutowirePass extends AbstractRecursivePass
{
private $definedTypes = array();
private $types;
private $ambiguousServiceTypes;
private $autowired = array();
private $lastFailure;
private $throwOnAutowiringException;
private $autowiringExceptions = array();
private $strictMode;
/**
* @param bool $throwOnAutowireException Errors can be retrieved via Definition::getErrors()
*/
public function __construct($throwOnAutowireException = true)
{
$this->throwOnAutowiringException = $throwOnAutowireException;
}
/**
* @deprecated since version 3.4, to be removed in 4.0.
*
* @return AutowiringFailedException[]
*/
public function getAutowiringExceptions()
{
@trigger_error(‘Calling AutowirePass::getAutowiringExceptions() is deprecated since Symfony 3.4 and will be removed in 4.0. Use Definition::getErrors() instead.’, E_USER_DEPRECATED);
return $this->autowiringExceptions;
}
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
// clear out any possibly stored exceptions from before
$this->autowiringExceptions = array();
$this->strictMode = $container->hasParameter(‘container.autowiring.strict_mode’) && $container->getParameter(‘container.autowiring.strict_mode’);
try {
parent::process($container);
} finally {
$this->definedTypes = array();
$this->types = null;
$this->ambiguousServiceTypes = null;
$this->autowired = array();
}
}
/**
* Creates a resource to help know if this service has changed.
*
* @param ReflectionClass $reflectionClass
*
* @return AutowireServiceResource
*
* @deprecated since version 3.3, to be removed in 4.0. Use ContainerBuilder::getReflectionClass() instead.
*/
public static function createResourceForClass(ReflectionClass $reflectionClass)
{
@trigger_error(‘The ‘.__METHOD__.'() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use ContainerBuilder::getReflectionClass() instead.’, E_USER_DEPRECATED);
$metadata = array();
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
if (!$reflectionMethod->isStatic()) {
$metadata[$reflectionMethod->name] = self::getResourceMetadataForMethod($reflectionMethod);
}
}
return new AutowireServiceResource($reflectionClass->name, $reflectionClass->getFileName(), $metadata);
}
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
{
try {
return $this->doProcessValue($value, $isRoot);
} catch (AutowiringFailedException $e) {
if ($this->throwOnAutowiringException) {
throw $e;
}
$this->autowiringExceptions[] = $e;
$this->container->getDefinition($this->currentId)->addError($e->getMessage());
return parent::processValue($value, $isRoot);
}
}
private function doProcessValue($value, $isRoot = false)
{
if ($value instanceof TypedReference) {
if ($ref = $this->getAutowiredReference($value, $value->getRequiringClass() ? sprintf(‘for «%s» in «%s»‘, $value->getType(), $value->getRequiringClass()) : »)) {
return $ref;
}
$this->container->log($this, $this->createTypeNotFoundMessage($value, ‘it’));
}
$value = parent::processValue($value, $isRoot);
if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
return $value;
}
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
$this->container->log($this, sprintf(‘Skipping service «%s»: Class or interface «%s» cannot be loaded.’, $this->currentId, $value->getClass()));
return $value;
}
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
$this->container->log($this, sprintf(‘Skipping service «%s»: Class or interface «%s» cannot be loaded.’, $this->currentId, $value->getClass()));
$methodCalls = $value->getMethodCalls();
try {
$constructor = $this->getConstructor($value, false);
} catch (RuntimeException $e) {
throw new AutowiringFailedException($this->currentId, $e->getMessage(), 0, $e);
}
if ($constructor) {
array_unshift($methodCalls, array($constructor, $value->getArguments()));
}
$methodCalls = $this->autowireCalls($reflectionClass, $methodCalls);
if ($constructor) {
list(, $arguments) = array_shift($methodCalls);
if ($arguments !== $value->getArguments()) {
$value->setArguments($arguments);
}
}
if ($methodCalls !== $value->getMethodCalls()) {
$value->setMethodCalls($methodCalls);
}
return $value;
}
/**
* @param ReflectionClass $reflectionClass
* @param array $methodCalls
*
* @return array
*/
private function autowireCalls(ReflectionClass $reflectionClass, array $methodCalls)
{
foreach ($methodCalls as $i => $call) {
list($method, $arguments) = $call;
if ($method instanceof ReflectionFunctionAbstract) {
$reflectionMethod = $method;
} else {
$reflectionMethod = $this->getReflectionMethod(new Definition($reflectionClass->name), $method);
}
$arguments = $this->autowireMethod($reflectionMethod, $arguments);
if ($arguments !== $call[1]) {
$methodCalls[$i][1] = $arguments;
}
}
return $methodCalls;
}
/**
* Autowires the constructor or a method.
*
* @param ReflectionFunctionAbstract $reflectionMethod
* @param array $arguments
*
* @return array The autowired arguments
*
* @throws AutowiringFailedException
*/
private function autowireMethod(ReflectionFunctionAbstract $reflectionMethod, array $arguments)
{
$class = $reflectionMethod instanceof ReflectionMethod ? $reflectionMethod->class : $this->currentId;
$method = $reflectionMethod->name;
$parameters = $reflectionMethod->getParameters();
if (method_exists(‘ReflectionMethod’, ‘isVariadic’) && $reflectionMethod->isVariadic()) {
array_pop($parameters);
}
foreach ($parameters as $index => $parameter) {
if (array_key_exists($index, $arguments) && » !== $arguments[$index]) {
continue;
}
$type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
if (!$type) {
if (isset($arguments[$index])) {
continue;
}
// no default value? Then fail
if (!$parameter->isDefaultValueAvailable()) {
// For core classes, isDefaultValueAvailable() can
// be false when isOptional() returns true. If the
// argument *is* optional, allow it to be missing
if ($parameter->isOptional()) {
continue;
}
throw new AutowiringFailedException($this->currentId, sprintf(‘Cannot autowire service «%s»: argument «$%s» of method «%s()» must have a type-hint or be given a value explicitly.’, $this->currentId, $parameter->name, $class !== $this->currentId ? $class.’::’.$method : $method));
}
// specifically pass the default value
$arguments[$index] = $parameter->getDefaultValue();
continue;
}
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, !$parameter->isOptional() ? $class : »), ‘for ‘.sprintf(‘argument «$%s» of method «%s()»‘, $parameter->name, $class.’::’.$method))) {
$failureMessage = $this->createTypeNotFoundMessage($ref, sprintf(‘argument «$%s» of method «%s()»‘, $parameter->name, $class !== $this->currentId ? $class.’::’.$method : $method));
if ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
} elseif (!$parameter->allowsNull()) {
throw new AutowiringFailedException($this->currentId, $failureMessage);
}
$this->container->log($this, $failureMessage);
}
$arguments[$index] = $value;
}
if ($parameters && !isset($arguments[++$index])) {
while (0 <= —$index) {
$parameter = $parameters[$index];
if (!$parameter->isDefaultValueAvailable() || $parameter->getDefaultValue() !== $arguments[$index]) {
break;
}
unset($arguments[$index]);
}
}
// it’s possible index 1 was set, then index 0, then 2, etc
// make sure that we re-order so they’re injected as expected
ksort($arguments);
return $arguments;
}
/**
* @return TypedReference|null A reference to the service matching the given type, if any
*/
private function getAutowiredReference(TypedReference $reference, $deprecationMessage)
{
$this->lastFailure = null;
$type = $reference->getType();
if ($type !== $this->container->normalizeId($reference) || ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract())) {
return $reference;
}
if (null === $this->types) {
$this->populateAvailableTypes($this->strictMode);
}
if (isset($this->definedTypes[$type])) {
return new TypedReference($this->types[$type], $type);
}
if (!$this->strictMode && isset($this->types[$type])) {
$message = ‘Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won’t be supported in version 4.0.’;
if ($aliasSuggestion = $this->getAliasesSuggestionForType($type = $reference->getType(), $deprecationMessage)) {
$message .= ‘ ‘.$aliasSuggestion;
} else {
$message .= sprintf(‘ You should %s the «%s» service to «%s» instead.’, isset($this->types[$this->types[$type]]) ? ‘alias’ : ‘rename (or alias)’, $this->types[$type], $type);
}
@trigger_error($message, E_USER_DEPRECATED);
return new TypedReference($this->types[$type], $type);
}
if (!$reference->canBeAutoregistered() || isset($this->types[$type]) || isset($this->ambiguousServiceTypes[$type])) {
return;
}
if (isset($this->autowired[$type])) {
return $this->autowired[$type] ? new TypedReference($this->autowired[$type], $type) : null;
}
if (!$this->strictMode) {
return $this->createAutowiredDefinition($type);
}
}
/**
* Populates the list of available types.
*/
private function populateAvailableTypes($onlyAutowiringTypes = false)
{
$this->types = array();
if (!$onlyAutowiringTypes) {
$this->ambiguousServiceTypes = array();
}
foreach ($this->container->getDefinitions() as $id => $definition) {
$this->populateAvailableType($id, $definition, $onlyAutowiringTypes);
}
}
/**
* Populates the list of available types for a given definition.
*
* @param string $id
* @param Definition $definition
*/
private function populateAvailableType($id, Definition $definition, $onlyAutowiringTypes)
{
// Never use abstract services
if ($definition->isAbstract()) {
return;
}
foreach ($definition->getAutowiringTypes(false) as $type) {
$this->definedTypes[$type] = true;
$this->types[$type] = $id;
unset($this->ambiguousServiceTypes[$type]);
}
if ($onlyAutowiringTypes) {
return;
}
if (preg_match(‘/^d+_[^~]++~[._a-zA-Zd]{7}$/’, $id) || $definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) {
return;
}
foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
$this->set($reflectionInterface->name, $id);
}
do {
$this->set($reflectionClass->name, $id);
} while ($reflectionClass = $reflectionClass->getParentClass());
}
/**
* Associates a type and a service id if applicable.
*
* @param string $type
* @param string $id
*/
private function set($type, $id)
{
if (isset($this->definedTypes[$type])) {
return;
}
// is this already a type/class that is known to match multiple services?
if (isset($this->ambiguousServiceTypes[$type])) {
$this->ambiguousServiceTypes[$type][] = $id;
return;
}
// check to make sure the type doesn’t match multiple services
if (!isset($this->types[$type]) || $this->types[$type] === $id) {
$this->types[$type] = $id;
return;
}
// keep an array of all services matching this type
if (!isset($this->ambiguousServiceTypes[$type])) {
$this->ambiguousServiceTypes[$type] = array($this->types[$type]);
unset($this->types[$type]);
}
$this->ambiguousServiceTypes[$type][] = $id;
}
/**
* Registers a definition for the type if possible or throws an exception.
*
* @param string $type
*
* @return TypedReference|null A reference to the registered definition
*/
private function createAutowiredDefinition($type)
{
if (!($typeHint = $this->container->getReflectionClass($type, false)) || !$typeHint->isInstantiable()) {
return;
}
$currentId = $this->currentId;
$this->currentId = $type;
$this->autowired[$type] = $argumentId = sprintf(‘autowired.%s’, $type);
$argumentDefinition = new Definition($type);
$argumentDefinition->setPublic(false);
$argumentDefinition->setAutowired(true);
try {
$originalThrowSetting = $this->throwOnAutowiringException;
$this->throwOnAutowiringException = true;
$this->processValue($argumentDefinition, true);
$this->container->setDefinition($argumentId, $argumentDefinition);
} catch (AutowiringFailedException $e) {
$this->autowired[$type] = false;
$this->lastFailure = $e->getMessage();
$this->container->log($this, $this->lastFailure);
return;
} finally {
$this->throwOnAutowiringException = $originalThrowSetting;
$this->currentId = $currentId;
}
@trigger_error(sprintf(‘Relying on service auto-registration for type «%s» is deprecated since Symfony 3.4 and won’t be supported in 4.0. Create a service named «%s» instead.’, $type, $type), E_USER_DEPRECATED);
$this->container->log($this, sprintf(‘Type «%s» has been auto-registered for service «%s».’, $type, $this->currentId));
return new TypedReference($argumentId, $type);
}
private function createTypeNotFoundMessage(TypedReference $reference, $label)
{
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), false)) {
// either $type does not exist or a parent class does not exist
try {
$resource = new ClassExistenceResource($type, false);
// isFresh() will explode ONLY if a parent class/trait does not exist
$resource->isFresh(0);
$parentMsg = false;
} catch (ReflectionException $e) {
$parentMsg = $e->getMessage();
}
$message = sprintf(‘has type «%s» but this class %s.’, $type, $parentMsg ? sprintf(‘is missing a parent class (%s)’, $parentMsg) : ‘was not found’);
} else {
$alternatives = $this->createTypeAlternatives($reference);
$message = $this->container->has($type) ? ‘this service is abstract’ : ‘no such service exists’;
$message = sprintf(‘references %s «%s» but %s.%s’, $r->isInterface() ? ‘interface’ : ‘class’, $type, $message, $alternatives);
if ($r->isInterface() && !$alternatives) {
$message .= ‘ Did you create a class that implements this interface?’;
}
}
$message = sprintf(‘Cannot autowire service «%s»: %s %s’, $this->currentId, $label, $message);
if (null !== $this->lastFailure) {
$message = $this->lastFailure.»n».$message;
$this->lastFailure = null;
}
return $message;
}
private function createTypeAlternatives(TypedReference $reference)
{
// try suggesting available aliases first
if ($message = $this->getAliasesSuggestionForType($type = $reference->getType())) {
return ‘ ‘.$message;
}
if (null === $this->ambiguousServiceTypes) {
$this->populateAvailableTypes();
}
if (isset($this->ambiguousServiceTypes[$type])) {
$message = sprintf(‘one of these existing services: «%s»‘, implode(‘», «‘, $this->ambiguousServiceTypes[$type]));
} elseif (isset($this->types[$type])) {
$message = sprintf(‘the existing «%s» service’, $this->types[$type]);
} elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered() && !$this->strictMode) {
return ‘ It cannot be auto-registered because it is from a different root namespace.’;
} else {
return;
}
return sprintf(‘ You should maybe alias this %s to %s.’, class_exists($type, false) ? ‘class’ : ‘interface’, $message);
}
/**
* @deprecated since version 3.3, to be removed in 4.0.
*/
private static function getResourceMetadataForMethod(ReflectionMethod $method)
{
$methodArgumentsMetadata = array();
foreach ($method->getParameters() as $parameter) {
try {
$class = $parameter->getClass();
} catch (ReflectionException $e) {
// type-hint is against a non-existent class
$class = false;
}
$isVariadic = method_exists($parameter, ‘isVariadic’) && $parameter->isVariadic();
$methodArgumentsMetadata[] = array(
‘class’ => $class,
‘isOptional’ => $parameter->isOptional(),
‘defaultValue’ => ($parameter->isOptional() && !$isVariadic) ? $parameter->getDefaultValue() : null,
);
}
return $methodArgumentsMetadata;
}
private function getAliasesSuggestionForType($type, $extraContext = null)
{
$aliases = array();
foreach (class_parents($type) + class_implements($type) as $parent) {
if ($this->container->has($parent) && !$this->container->findDefinition($parent)->isAbstract()) {
$aliases[] = $parent;
}
}
$extraContext = $extraContext ? ‘ ‘.$extraContext : »;
if (1 < $len = count($aliases)) {
$message = sprintf(‘Try changing the type-hint%s to one of its parents: ‘, $extraContext);
for ($i = 0, —$len; $i < $len; ++$i) {
$message .= sprintf(‘%s «%s», ‘, class_exists($aliases[$i], false) ? ‘class’ : ‘interface’, $aliases[$i]);
}
$message .= sprintf(‘or %s «%s».’, class_exists($aliases[$i], false) ? ‘class’ : ‘interface’, $aliases[$i]);
return $message;
}
if ($aliases) {
return sprintf(‘Try changing the type-hint%s to «%s» instead.’, $extraContext, $aliases[0]);
}
}
}